How to deploy a Django app on Heroku. Part III

This is the third part of a series on how to deploy a Django app on Heroku.

How to deploy a Django app on Heroku. Part I.

How to deploy a Django app on Heroku. Part II.

You will see that the main idea behind these posts is not to build a functional Django app. Instead, what I want to do is to give you a bunch of good practices on how to build a Django app with different environments for testing, developing and production, useful packages installed, Internationalization and Localization properly configured, PosgreSQL installed, and much more.

PART III

The topics covered here are:

  • configure PostgreSQL in your local machine
  • configure PostgreSQL in Heroku
  • syncdb and Django shell in Heroku

PostgreSQL Database – Local

Let us talk about setting properly the PostgreSQL database on Heroku and on your local computer.

This tutorial assumes you have Postgres installed on your computer. If you have a Mac with OS X Mountain Lion, you might want to see How to set a Django app on Heroku. Part I. I you have Linux or Windows, you might want to see the official Heroku post.

On mac, the command

$ which psql

should point to the Postgres app. And this other command should start the Postgres command line utility:

$ psql -h localhost

If this command rises an error like “psql: FATAL: database <user> does not exist”, it might be because your package manager failed to create the proper database, see this post for more info. To solve this problem, try:

$ createdb

$ psql -h localhost

Now, you should be in the command line of Postgres. You can exit this environment by typing

\q

Similarly, you can type \? for more help. Other useful commands are: \list to list your databases and \du to list your users.

Configuring the local database for your Django app: We need to create a new database for our app and a new user to whom you will give access to the database:

$ createdb mylocaldb

$ psql

# CREATE ROLE myusername WITH LOGIN PASSWORD ‘mypassword’;

# GRANT ALL PRIVILEGES ON DATABASE mylocaldb TO myusername;

Open both your development_settings.py and test_settings.py files and add the following lines:

DATABASES = {
    ‘default’: {
        ‘ENGINE’‘django.db.backends.postgresql_psycopg2’
        ‘NAME’‘taskbusterdb’,  
        ‘USER’: ‘myusername,
        ‘PASSWORD’‘mypassword’,
        ‘HOST’,  
        ‘PORT’
    }
}

After this, you should be able to sync your database with:

$ python manage.py syncdb

remember to have your development environment active.

PostgreSQL Database – Heroku

On Heroku, you can check if your application already has a database provisioned and what plan it is (if it is free or payed) with:

$ heroku addons | grep POSTGRES

If you don’t have any database provisioned, the terminal won’t show any message. Otherwise, you will see something like heroku-postgresql:dev HEROKU_POSTGRESQL_RED. See the official post for more information.

To create a new free postgres database (dev option) attached to your Heroku application:

$ heroku addons:add heroku-postgresql:dev

Once Heroku Postgres has been added, there will be a HEROKU_POSTGRESQL_COLOR_URL setting available in the app configuration (in my case COLOR was YELLOW). This variable will contain the url used to access the new Postgresql service on Heroku. This can be confirmed using

$ heroku config | grep HEROKU_POSTGRESQL

you should see the corresponding url. Heroku recomends using the variable DATABASE_URL to store the location of your primary database. Hence, the config variable created before must be promoted to this new config variable:

$ heroku pg:promote HEROKU_POSTGRESQL_YELLOW_URL

To see all PostgreSQL databases provisioned by your application and its characteristics, use

$ heroku pg:info

To establish a psql session, the native PostgreSQL interactive terminal, with your remote database use

$ heroku pg:psql

More useful commands for your database

There are some useful commands that can be used to control your Postgres database.

Pull can be used to pull data from your remote Heroku Postgres database to your local machine database.

$ heroku pg:pull HEROKU_POSTGRESQL_YELLOW mylocaldb –app myapponheroku

This command takes the Heroku database of the app myapponheroku and saves its data to a local database in your machine, named mylocaldb. If the local database already exists, you will be asked to remove it before proceeding. The previous command is equivalent to:

$ heroku pg:pull myapponheroku::YELLOW mylocaldb

Push is the inverse of pull, and takes data from your local database and push it to the database in Heroku:

$ heroku pg:push mylocaldb HEROKU_POSTGRESQL_YELLOW –app myapponheroku

If the remote database is not empty, you will be prompted to reset it with

$ heroku pg:reset HEROKU_POSTGRESQL_YELLOW

Syncdb and Django shell

Before proceeding, I recommend to upload your changes on Heroku. Recall that you can do this with:

$ git add .

$ git commit -m “PostgreSQL set”

$ git push heroku master

Make sure your app is still working on your local machine and on Heroku.

To sync the Django models, you can use the heroku run command, which lets you run one-off admin dynos.

$ heroku run python manage.py syncdb

In a similar way, you can use the Django shell:

$ heroku run python manage.py shell

 

This is the end of this post.

The next part is available here: How to deploy a Django app on Heroku. Part IV.

Click on the g+1 if you found this post useful! 🙂

Google+TwitterLinkedInFacebookReddit

Please, add +Marina Mele in your comments. This way I will get a notification email and I will answer you as soon as possible! :-)