url = urlparse.urlparse(os.environ['DATABASE_URL'])
conn = psycopg2.connect('dbname=%s user=%s password=%s host=%s ' % (url.
    path[1:], url.username, url.password, url.hostname))
cur = conn.cursor()
query = 'SELECT ....'
cur.execute(query)
Python database WITHOUT using Django (for Heroku)