I am exploring various features of the Python language. I have created a Postgres db on Heroku, am am looking to connect to it. I have the host, database user, port and password settings. I am not looking to deploy to Heroku, just connect locally to this db. Where can I get started?
2 Answers
From Heroku Postgres doc, with psycopg2 :
To use PostgreSQL as your database in Python applications you will need to use the psycopg2 package.
pip install psycopg2-binaryAnd use this package to connect to DATABASE_URL in your code.
import os import psycopg2 DATABASE_URL = os.environ['DATABASE_URL'] conn = psycopg2.connect(DATABASE_URL, sslmode='require')