import psycopg2
conn = psycopg2.connect(database='yourdb', user='dbuser', password=
    'abcd1234', host='server', port='5432', sslmode='require')
How to connect to a remote PostgreSQL database with Python