0

So it took me (several) hours yesterday to even have the boiler plate flask web application up on running for Project 1.

Here I will be adding my notes on what went wrong and how I fixed it. In a nushell, there are some not so obvious problems,, that I'll walk through.

Please note that I have a local postgres instance(v. 11) running on my laptop, and I am using a MacOS.

1

Here are the stumbling blocks I faced:

SQLAlchemy Dependencey Issues:

  1. Postgres path not set I have written about this in another question (see link), but in a nutshell you need to add the path to postgres binaries otherwise SQLAlchemy will not install properly.
  2. psycopg2 has issues on my version of MacOS, and by issues I mean it would not install via pip - the warinings recommend installing the binary version instead (pip3 install psycopg2-binary).

Adding the DATABASE_URL

Adding the DATABASE_URL is more complicated than described. In the videos how to to get the code connected to a DB went by fast, it noted about adding the DB URL in an enviromental variable, and that is it. Below I have included:

  • How to build the URL for your database
  • Add the database URL as an enviromental variable
  • Explain why add the database_url as an envriomental variable

How to build the URL for your database:

What wasn't clearly explained is that a DB URL is a combination in the database type, the hostname, network port, username&password & database.

Heroku has a the full link built, but if you want to run it on your local host, you need to develop your own. I found this code (see link) that was helpful.

           POSTGRES = {
            'user': 'postgresuser',
            'pw': 'examplepassword',
            'db': 'my_database',
            'host': 'localhost',
            'port': '5432',
        }
databaseurl = 'postgresql://%(user)s:%(pw)s@%(host)s:%(port)s/%(db)s' % POSTGRES
print(databaseurl)

The result should look like this: postgresql://postgresuser:examplepassword@localhost:5432/my_database

How to add Database URL as an enviromental variable?

  • Open bash profile e.g. : atom ~/.bash_profile (note you can use any text editor - I use atom)
  • Add DATABASE_URL & save: add the following line export DATABASE_URL="postgresql://postgresuser:examplepassword@localhost:5432/my_database".
  • Apply change: Run the command source ~/.bash_profile
  • Test change: Run the command echo $DATABASE_URL

Why add the database_url as an envriomental variable in the first place?

The two simple reasons are 1) Security 2) Best practice You do not want to add passwords and secrets like API keys in your code, so it makes sense for the app to pull the database_url directly from an environmental variable. Depending on your security needs you might want to have that password in a vault (but for this excerise, an env variable is fine).

Another thing to note, is that that database_url will change from enviroment to enviroment so hardcoding it in code my not be as advidable as in a config file/ env variable.

You must log in to answer this question.

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