Skip to content

Instantly share code, notes, and snippets.

@pulse-
Created January 27, 2014 19:44
Show Gist options
  • Save pulse-/8655893 to your computer and use it in GitHub Desktop.
Save pulse-/8655893 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.
@svedova
Copy link

svedova commented Jun 15, 2017

If you are using django >= 1.9 syncdb is removed. Use migrate instead.

@vuongphu
Copy link

it working , thanks

@bipindr123
Copy link

Thanks a ton mate!

@chinjj
Copy link

chinjj commented Aug 31, 2017

If current data in sqlite3 was migrated over to PostgreSQL, will my current code crash or conflict?

@Pradhvan
Copy link

@DogukanSAVA
Copy link

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment