Django: dumping and loading databases

At work , we have this situation where we want to dump the database from the production servers so that we can work with that data on our local development machines. As part of an internal app (which another developer is working on open-sourcing parts of, UPDATE: the app is on github, however I no longer use it myself, not sure if it’s still used internally at Trapeze), we have two management commands to help us with this: load_devdata and dump_devdata.

The reason we use a management command is because we don’t install Fabric on our production servers to run these commands, we don’t use shell scripts either.

The database commands are PostgreSQL-specific and we use rsync to copy the media files.

The code hasn’t been refactored what we all the work we have to do for clients, however it does work. There might be some code missing to be able to run these snippets, but I think this is okay since your own workplace or production environment might be different; you might be using a different database, not using geospatial database extensions, or not interested in copying the media files. Think of the snippets as inspiration.

For more Django best practices, click here to check out “Two Scoops of Django”

Dumping the database

The dump command will duplicate the database and the files. You can then zip these up and transfer them to a local machine. 99% of the time we just commit the database and media files to our repository.

Loading the database dump

The load command will create a database and then load up the database dump, it also copies the media files back to the media directory.

Author: Rudolf Olah

Rudolf Olah is a software development expert with over 6 years of professional software developer experience. He has produced the video course Learning AngularJS Testing for PacktPublishing and works on the strategic as well as the tactical parts of software and web devleopment.

Leave a Reply and Share Your Thoughts