Created
December 7, 2017 00:44
-
-
Save xtornasol512/3b3e50ced0c79e86dc4e0ca0bece9351 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' 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