How to Setup Postgresql (Python-flask) App on Heroku and Local Machine

| Comments

So I was like setting up a python web app on Heroku cloud platform and it was awesome. You don’t need to break a sweat to deploy something like a ROR app but things are different when it come to micro-frameworks like Python-flask.
I chose flask for the beauty and simplicity, and because I wanted to do everything myself. The app has postgres-sql as it’s database backend. Deploying apps on Heroku is easy and all but there are some things which are not that easy to find in official docs and tutorials.
So here we shall proceed with setting up postgres (with Python) on Heroku.

Install flask-sqlalchemy and psycopg2

Since we are using python we need these libraries for accessing the services. flask-sqlalchemy is a flask extension for the infamous sqlalchemy library and we need psychopg2 to interact with the postgres itself.
If you are using other language the you obviously need to use relative tools.
Above libraries can be installed with
pip install flask-sqlalchemy psychopg2
on your local setup. Then do
pip freeze > requirements.txt
This will put the newly installed libraries in requirements.txt file so they will get automagically installed on your Heroku instance when you push the code.

Might need to install this on local machine to compile psycopg2

python-dev
Before installing psycopg2 on your local setup, you should install python-dev tools on your local linux box.

Add heroku postgres-dev addon (it provide 10k rows for free)

Heroku provides the database service as an addon to the standard platform. So we need to add the addon to our setup to use postgres.

Run this command on your local terminal (obviously you need to have heroku-belt installed)

heroku addons:add heroku-postgresql:dev

After this the database is setup, now promote it to $DATABASE_URL

Database is now setup. But remember how we access the database on non-cloud (traditional they call it) services? Yes, we need the url to access database (it’s something like ”postgresql://pguser:password/dbname”). Heroku creates an environment variable with a color name (you will see it when you add the addon).
We need to use heroku-belt’s pg:promote command to enable $DATABASE_URL environment variable.
heroku pg:promote HEROKU_POSTGRESQL_COLOR

Now set SQLALCHEMY_DATABASE_URI in flask app

Now we have the database URI. We need to access it in the database. We can do this by simply accessing the $DATABASE_URL environment variable. This is how to do it with a flask app.
SQLALCHEMY_DATABASE_URI = os.environ.get(“DATABASE_URL”,”postgresql://pguser:password/dbname”)

This has an extra hack for accessing the local database. It loads heroku database url in heroku and local url in local dev environment. You obviously need to modify it with your local setup.
Another way to use local database is to export the local database URI to the shell environment. Put this line in your .zshrc or .bash_profile (depending on the shell you are using)
export DATABASE_URI=”postgresql://pguser:password/dbname”

Create the postgres database on heroku

Here’s the python flask specific stuff now. In flask, unless you are using something like ’alembic’ for database migrations, you can directly create your database on Heroku.

Just run python console attached to your Heroku instance
heroku run python
Then do import your flask-sqlalchemy instance and create the database with
>>> db.create_all
assuming that ‘db’ is the flask-sqlalchemy instance.


So here we are with our heroku app ready to rock the postgres.

(Phewww…I eventually managed to write a post after so long. Such relief.)




Circle Beat Of The Geek on Google Plus
OR Like us on Facebook  OR Follow on Twitter

Comments