Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting Python to a Heroku PostgreSQL DB?

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?

like image 477
AndroidDev Avatar asked May 24 '26 09:05

AndroidDev


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-binary

And 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')
like image 135
freezed Avatar answered May 25 '26 21:05

freezed


I don't know anything about Heroku, but maybe psycopg is what you're looking for?

like image 39
Petter Engström Avatar answered May 25 '26 22:05

Petter Engström