Database Reference
In-Depth Information
Connecting with Python
In order to use PostgreSQL as database in your Python applications it is necessary to use
the psycopg2 package and add it in the dependency file called requirements.txt .
The following is just a code sample to help you understand the concept;
$ pip install psycopg2
$ pip freeze > requirements.txt
Then, use the psycopg2 package to connect the DATABASE_URL variable:
import psycopg2
import urlparse
import os
try:
urlparse.uses_netloc.append("postgres")
connection_params =
urlparse.urlparse(os.environ["DATABASE_URL"])
db_connection = psycopg2.connect(database =
connection_params.path[1:], user =
connection_params.username, password =
connection_params.password, host =
connection_params.hostname, port = connection_params.port)
except:
print "Database connection failed."
Search WWH ::




Custom Search