title: Connecting to my RoR's Heroku Postgres database?
body: <p>I am running a Ruby on Rails app on Heroku.  It uses a Postgres database.  I want to connect to the database from some scripts I am writing and running locally.</p>
  
  <p>Using ENV['DATABASE_URL'] I found my connection url to be:</p>
  
  <p><strong>postgres://zugzagcaht:h-HebaxOz1_H-5uq_Olv@ec2-25-25-209-55.compute-1.amazonaws.com/zugzagcaht</strong></p>
  
  <p>(I modified the characters so this is not the real string!)</p>
  
  <p>Now I am running a Python script that should connect to the database, but am finding a timeout in the connection:</p>
  
  <pre><code>import psycopg2
  
  DB_NAME = 'zugzagcaht'
  DB_USER = 'zugzagcaht'
  DB_HOST = 'ec2-25-25-209-55.compute-1.amazonaws.com'
  DB_PASSWORD = 'h-HebaxOz1_H-5uq_Olv'
  TIMEOUT = 5 # seconds
  
  conn = None
  try:
    conn = psycopg2.connect("connect_timeout=%i dbname='%s' user='%s' host='%s' password='%s'" % (TIMEOUT, DB_NAME, DB_USER, DB_HOST, DB_PASSWORD))
  except:
    print "I am unable to connect to the database"
  
  if conn:
    cur = conn.cursor()
    cur.execute("SELECT datname from pg_database")
    rows = cur.fetchall()
    print "Here's the databases:\n"
    for row in rows:
        print "   ", row[0]
  </code></pre>
  
  <p>Any idea why this isn't working?</p>