Django: How to synchronize the database of different environments

syncing django development, staging, and production environment databases
Updated: November 22, 2022 Published:  June 15, 2022

If you work with multiple environments and have separate databases you probably want to synchronize your databases at some point.

Django automatically creates migration if the scheme changes and you can manually create data migrations. However, if you do changes in the admin panel, those migration files are not generated automatically.

In the following, I will explain the DB sync process for these data changes in detail. Make sure to read carefully since it involves critical steps.

1. Export Data from Django Database

In order to do so, you can pull all the data from the database into a JSON file with the dumpdata command.

Attention: Be careful with the exports of all data, also sensitive information like sessions or user data including passwords will be exported. To prevent the export of those you can add the following flags to the dumpdata command:

--exclude=auth --exclude=sessions

By default, all the data will be exported in a single line. Therefore it is recommended to pretty-print the JSON file in most cases. This way it is human-readable and easier to double-check. The following flag will add new lines and indentions to the output file:

--indent 2

2. Import Data into Django Database

To update the database of a different environment with your dumped data you can use the loaddata command. This commands loads everything from the JSON file into the database. If the data is already in the database, meaning the primary key is the same, the data will be overwritten with the data from the JSON file. In case the data is in the database but not in the JSON file, the data will remain untouched.

Do you want to ensure that only the JSON file data is in the DB? Then you have to flush the database beforehand:

# to delete all the data
python manage.py flush

# to delete the data of the specified database
python manage.py flush --database DATABASE 

3. Conclusion

These commands will export the data from one database and then import the data from the exported file to another database:

python manage.py dumpdata --exclude=auth --exclude=sessions \
--indent 2 > db.json

python manage.py loaddata db.json

If you wish to automate this task, you can make use of Git Hooks.

Happy Coding!

Leave a comment

Your email address will not be published. Required fields are marked *