2017-01-18 68 views
1

我想將ElephantSQL與Python連接起來。將ElephantSQL與Python連接

import os 
import psycopg2 
import urlparse 

urlparse.uses_netloc.append("postgres") 
url = urlparse.urlparse(os.environ["MY_DATABASE_URL"]) 

conn = psycopg2.connect(database=url.path[1:], 
    user=url.username, 
    password=url.password, 
    host=url.hostname, 
    port=url.port 
) 

,但我得到的錯誤:

Traceback (most recent call last): File "test.py", line 217, in url = urlparse.urlparse(os.environ["MY_DATABASE_URL"]) File "/usr/lib/python2.7/UserDict.py", line 23, in getitem raise KeyError(key) KeyError: 'MY_DATABASE_URL'

爲my_database_url我寫的網址從ElephantSQL(Postgres的:// my_username:MY_PASSWORD @ my_hostname/my_databasename)

我在做什麼錯?

+1

該密鑰在您的環境中不存在 –

回答

1

我用:

conn = psycopg2.connect("dbname='my_dbname' user='my_user' host='my_host' password='my_password'") 

,它的工作現在。