I'd like to connect ElephantSQL with 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
)
but i get error:
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'
as my_database_url i write url from ElephantSQL (postgres://my_username:my_password@my_hostname/my_databasename)
What am I doing wrong?
I used:
conn = psycopg2.connect("dbname='my_dbname' user='my_user' host='my_host' password='my_password'")
and it's working now.