title: Connecting Heroku Database to SQLAlchemy
body: <p>so as part of a Web Programming MOOC, I have to connect to a PostgreSQL Database hosted on Heroku using Python and SQLalchemy. I have spent a lot of hours trying to do this to no avail. My main problem is that I'm not being able to connect to the Database, because every time a try to run a script to update or just check the Database I get <em>"Is the server running on host "ec2-174-129-35-61.compute-1.amazonaws.com" (174.129.35.61) and accepting
  TCP/IP connections on port 5432?"</em> in the command prompt.</p>
  
  <p>I've done a lot of research trying to figure it out and it seems that maybe the URI of my Database is not set to accept public connections. But I tried setting ssl = require and still haven't been able to solve the problem. Maybe I didn't do it right?</p>
  
  <p>I have come to think that maybe I am doing something wrong with the code, or that I should try to connect through Heroku's CLI and not the command prompt but I am not sure. This is my simple test code: </p>
  
  <pre><code>import os
  from sqlalchemy import create_engine
  from sqlalchemy.orm import scoped_session, sessionmaker
  
  if not os.getenv("DATABASE_URL"):
      raise RuntimeError("DATABASE_URL is not set")
  
  engine = create_engine(os.getenv("DATABASE_URL"))
  db = scoped_session(sessionmaker(bind=engine))
  
  db.execute("INSERT INTO books VALUES (1, 1, 1, 1)")
  db.commit()
  </code></pre>
  
  <p>I set the DATABASE_URL environment variable in the command prompt to the Database's URI but maybe I did it wrong? I also tried just using the URI in the code itself but still could not make it work.</p>
  
  <p>This is my first time using Databases with Heroku so I am complete neewbie, maybe I am doing something else wrong?. I appreciate all the help that you guys can give. Thanks in advance</p>