Skip to content

Instantly share code, notes, and snippets.

@xtornasol512
Created December 7, 2017 00:44
Show Gist options
  • Save xtornasol512/3b3e50ced0c79e86dc4e0ca0bece9351 to your computer and use it in GitHub Desktop.
Save xtornasol512/3b3e50ced0c79e86dc4e0ca0bece9351 to your computer and use it in GitHub Desktop.
''' Method to connect to a database url(URI) with psycopg2 '''
import os
import psycopg2
import urlparse # import urllib.parse for python 3
result = urlparse.urlparse(os.environ["DATABASE_URL"])
username = result.username
password = result.password
database = result.path[1:]
hostname = result.hostname
connection = psycopg2.connect(
database = database,
user = username,
password = password,
host = hostname
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment