Managing a Django Database on Heroku
February 14, 2012Whether you are using Heroku’s database offerings in development or production, you’ll most likely need to push data and make changes to table schemas. I used these methods in the context of managing a database for a Django app, but they should work for apps built on other frameworks as well.
Initially, I followed the method laid out here, but I didn’t like having to add and remove the data from the repo.
Heroku recommends using a ruby gem called Taps to manage your data. Even though Taps is a ruby library, it works just fine with Django since its just interacting with your local database of choice and doesn’t much care about what the database is used for.
Heroku put together a good article on using Taps. Given that the Heroku walk through is pretty comprehensive, you should read that first before reading the rest of this post. This article is meant to summarize my experiences and act as a Django supplement to the Heroku article. Also, this article was particularly helpful through-out this whole process. I recommend giving it a read before getting started.
To use Taps, you’ll need ruby on your local machine. You’ll also need to install the appropriate database gem for Ruby:
Mysql
Postgres
Also, since there is no .yml for a Django project, you’ll need to use the database URL parameters for all of the Taps commands or create a .yml file. For my project, I was using Postgres, so I’ll use those for the examples. The mysql database URL structure is a little different. See the Heroku Taps article for an example.
Push local database to Heroku
This will overwrite your database on Heroku. Adding the confirm parameter will skip the confirmation from Heroku. If you want to play it safe and make this a two-step process, leave it off.
Backing up Heroku database
First, create a local database to store the backup tables in:
Then, run:
Modifying the schema of the Heroku database
I highly recommend using South for database migrations in Django. With South, you’ll be able to make and keep track of changes to all of your app’s models pretty easily. Follow this tutorial to get started.
Once you have South up and running locally, you can update the Heroku database schemas just like you would on your local machine. Make sure the South migration files are being tracked in your git repo so that they get pushed to Heroku. Then run syncdb
on Heroku to add the necessary South tables to your remote database. Now you can run migrations on Heroku just like you would locally. For example:
Use a remote database for your Heroku app
When you are ready to move your database off Heroku (I think you can find a better deal on a cloud database than using the Heroku options), you’ll need to modify your settings.py to point to a remote server. After getting your remote DB up and running, run this command
If you don’t have this line in your settings.py, Heroku adds this line automatically and points it at it’s own servers. Adding this line yourself allows you to specify which database Heroku should look to for your app.