When you need to move data between your dev machine and the server, or back up before a risky migration, don’t copy the database file or write your own insert script. Django already ships two management commands that do exactly this: dumpdata exports all or part of your data to a JSON file, and loaddata loads that file back into another database. Two commands, and the data arrives intact from one machine to the other.
The Problem
A familiar situation: the blog has 200 posts on your dev machine and you need them on staging to test a new layout. Or you’re about to run a migration that changes a column type and want a backup to roll back to if it goes wrong. The habitual fix is copying db.sqlite3.
# Only works with SQLite — it won't move to PostgreSQL or MySQLcp db.sqlite3 db.sqlite3.bak
This approach has three weaknesses: it only works with SQLite, it can’t select a single app or model, and it’s useless when the two machines use different database engines. Writing a Python script to read and re-insert rows yourself is no better: verbose, easy to forget foreign keys, easy to skew primary keys.
The Solution
dumpdata exports data as JSON (the default), each record holding the model name, primary key and fields. Export a single model:
python manage.py dumpdata blog.Post --indent 2 > posts.json
To back up the whole database, drop the model name — but exclude two groups of system data: permissions (auth.permission) and content types (contenttypes), since both are recreated on migrate:
python manage.py dumpdata --exclude auth.permission --exclude contenttypes --indent 2 > backup.json
To restore, run migrate first so the tables exist, then load the file:
python manage.py migratepython manage.py loaddata posts.json
loaddata looks for the file in each app’s fixtures directory, in FIXTURE_DIRS if you configure it, or accepts an absolute path. When the two databases are different engines (say SQLite to PostgreSQL), add --natural-foreign and --natural-primary when dumping so foreign keys are written as natural keys. Note: models you write yourself need a natural_key() method for these flags to have any effect.
Apply It to Your Project
Build these two commands into your routine: before every risky migration, run dumpdata and keep the backup next to the code; when a new dev clones the project, ship a sample-data fixture file instead of telling them to type data in by hand; in CI, load fixtures with loaddata so tests always run on the same dataset. Data becomes a text file in the repo — diffable, and restorable with a single command.
Related Posts
- Django: Writing Custom Management Commands — Seed Data and Run Background Tasks in 5 Minutes
- Django: Use Raw SQL Safely When the ORM Isn’t Enough — objects.raw() and cursor() in 5 Minutes
- Django: Test Views with TestCase and Client — Catch Bugs Before Deploy in 5 Minutes
FAQ
Can I back up by copying the db.sqlite3 file?
Yes, but only when both machines use SQLite and you need the whole database. With PostgreSQL or MySQL there is no file to copy, and you can’t select a single app or model either. dumpdata handles both.
Why does loaddata report duplicate primary keys?
Because the target database already has records with the same primary key — usually from loading a fixture twice, or from a database that wasn’t emptied. Fix it by loading into an empty database after migrate, or use flush to clear old data first. Remember to run migrate first: if the tables don’t exist, loaddata fails immediately.
Does loaddata call the model’s save() and signals?
No. loaddata writes straight to the database, skipping any save() method you defined; pre_save/post_save signals still fire, but with raw=True. If important logic lives in save() or a signal, check the data after loading.
Can dumpdata move data from SQLite to PostgreSQL?
Yes, if you dump with --natural-foreign --natural-primary so foreign keys stop being numeric ids. Models you write yourself need a natural_key() method for these flags to work. For large datasets the JSON file can get heavy — use pg_dump instead.
💬 Have you tried this in your own project? If you hit an error or have a shorter way, leave a comment below — I read them all and will fold the good ideas into the post.
References
- Django Documentation — dumpdata and loaddata: https://docs.djangoproject.com/en/6.1/ref/django-admin/#dumpdata
- Django Documentation — Serializing Django objects: https://docs.djangoproject.com/en/6.1/topics/serialization/
- Django Documentation — Providing initial data with fixtures: https://docs.djangoproject.com/en/6.1/howto/initial-data/
