import urllib.parse
result = urllib.parse.urlparse('postgresql://postgres:postgres@localhost/postgres')
username = result.username
password = result.password
database = result.path[1:]
hostname = result.hostname
connection = psycopg2.connect(database=database, user=username, password=
    password, host=hostname)
Connect to an URI in postgres