Django: What are the best practices to migrate a project from sqlite to PostgreSQL

Pierre-Jean Coudert picture Pierre-Jean Coudert · Aug 13, 2010 · Viewed 25.6k times · Source

I need to migrate a complex project from sqlite to PostgreSQL. A lot of people seems to have problem with foreign keys, data truncature and so on...

  • Is there a full automated utility ?
  • Do I need to check some data or schema before the migration ?

Edit : I tried django-command-extensions DumpScript but it doesn't run on my 2GB RAM PC with my current DataSet.

Answer

Nimo picture Nimo · Dec 8, 2014

In my experience, dumping & restoring from SQL doesn't work properly.

You should follow this sequence instead:

1. Dump db contents to json

$ ./manage.py dumpdata > dump.json

2. Switch the backend in settings.py

DATABASES = {
    # COMMENT OUT:
    # 'default': dj_database_url.config(default='sqlite:////full/path/to/your/database/file.sqlite'),
    # ADD THIS INSTEAD:
    'default': dj_database_url.config(default='postgres://localhost:5432/postgres_db_name'),
}

3. Syncdb and migrate the new DB to the same table structure

$ ./manage.py syncdb
$ ./manage.py migrate

4. Load the json to the new db.

$ ./manage.py loaddata dump.json

5. Congrats! Now the new data is in your postgres db.