Skip to content

Instantly share code, notes, and snippets.

@claraj
Created April 19, 2017 19:20
Show Gist options
  • Save claraj/caa2478571da264279c51ce6818439fd to your computer and use it in GitHub Desktop.
Save claraj/caa2478571da264279c51ce6818439fd to your computer and use it in GitHub Desktop.
import os
import psycopg2
from urllib.parse import urlparse
#https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-python
p_url = urlparse(os.environ["DATABASE_URL"])
url = urlparse(p_url)
conn = psycopg2.connect(
database=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port
)
print('Connected to postgres.')
# Todo do database stuff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment