I am trying to import the books.csv file into the online PostgreSQL database via Python code, as shown in the lectures and I get this error message when sqlalchemy is attempting to create the engine. I have substituted the "DATABASE_URL" in the code with the correct url of the database.

This also happens when trying to run any of the Python files provided in the course. When connecting directly to the database with the psql URL command, I can run any query and manipulate the tables.

As per project instructions, this error message occurs when not setting the DATABASE_URL environment variable to be the URI of the database, although related to running flask. The variable is set (I have checked for the environment variables). What am I missing?

Thank you in advance for any help.

  • Does psql $DATABASE_URL work? Jul 19 '18 at 2:31
  • Thanks for answering. Yes, it works fine. I get connected to the database right away. Also, I am able to run flask from the terminal and get to the page "Project1: TODO". I have tried to run the Python file with code checking for the environment variable and it throws the RuntimeError "DATABASE_URL is not set".
    – mig5
    Jul 19 '18 at 11:04
  • "I have substituted the "DATABASE_URL" in the code with the correct url of the database. ". Does this mean you have "hard coded" the db URL instead of using os.getenv? Jul 19 '18 at 13:28
  • Well, stupidly enough I did, because I was trying to run the code from inside the Python file with F5, but I forgot to remove os.getenv. I completely overlooked the point of using it. Well, better late than never. Thank you for taking the time to look into this.
    – mig5
    Jul 19 '18 at 21:53
  • Oops, my case is that I typed the wrong name for DATABASE_URL...
    – user419050
    Nov 25 '18 at 19:31

I had to set up the environmental variable again ... not sure the reason why since it was already set up before, do we need 1 per terminal instance?

Debug: I have added


to both files import.py and application.py understand what was wrong.

Since I had a debug flask server running on 1 terminal the output of application.py DATABASE_URL was correct.


However, the output from import.py was = None ...

Solution: I have set up the environmental variable again on this terminal where I was running $python3 import.py

Export DATABASE_URL = ".... (insert the heroku link here for your postgres)..."

and it's now working ...

Hope it helps

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .