Skip to content

Instantly share code, notes, and snippets.

@n37r06u3
Forked from pulse-/gist:8655893
Created June 30, 2017 07:54
Show Gist options
  • Save n37r06u3/78eb88ea444822ee04ddf0574065d54e to your computer and use it in GitHub Desktop.
Save n37r06u3/78eb88ea444822ee04ddf0574065d54e to your computer and use it in GitHub Desktop.
Django migrate sqlite3 db to postgres - The easy way.
I had this really small problem today. I wanted to migrate one of my small django apps to use postgres, just to make everything easy to manage. Sqlite3 is perfectly fine for the amount of load, however I am really much faster at administering postgres than I am on sqlite3. So I decided to migrate the stuff over.
I tried a few approaches, but what ultimately worked the best and the fastest fo rmy particular problem was to do the following.
Use original SQLITE3 connection in settings.py
1. python manage.py dumpdata > dump.json
(I read some things here about some options you can pass, at the end what just worked was the following)
2. Change DB connection string in settings.py to POSTGRES
3. python manage.py syncdb - Answer no to everything
4. python manage.py loaddata data.json - Big FAIL something about duplicate values.
5. login to postgres db using psql
6. execute this to delete all the data in the content_types table
truncate django_content_type RESTART IDENTITY CASCADE;
7. python manage.py loaddata data.json
8. =)
This will work for pretty much any datasource.
Your mileage may vary but it was a quick hack and it solved my problem very quickly.
Good luck.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment