Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 295
Pierre-Jean Coudert Avatar asked Aug 13 '10 12:08

Pierre-Jean Coudert


People also ask

How do I migrate from SQLite to Postgres?

Steps for Connecting SQLite to PostgreSQLStep 1: Create SQLite DB Dumpdata Backup. Step 2: Generate a Postgres DB and User. Step 3: Configure Settings.py. Step 4: Import Required Fixture via Loaddata from SQLite to PostgreSQL.

What is the best way to transfer the data in a PostgreSQL?

If you really have two distinct PostgreSQL databases, the common way of transferring data from one to another would be to export your tables (with pg_dump -t ) to a file, and import them into the other database (with psql ).

Which is better SQLite or PostgreSQL?

SQLite doesn't perform well when it comes to user management. It also lacks the ability to handle simultaneous access by multiple users. PostgreSQL performs very well in managing users. It has well-defined permission levels for users that determine what operations they can perform in the database.


2 Answers

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.

like image 123
Nimo Avatar answered Sep 24 '22 02:09

Nimo


The following is a refinement of Nimo's answer and Stephen's answer for Django 1.7+:

  1. ./manage.py dumpdata --natural-primary --natural-foreign > dump.json
  2. Change DATABASES in settings.py to point to the new (PostgreSQL) db.
  3. ./manage.py migrate
  4. ./manage.py loaddata dump.json

One problem I encountered is that SQLite doesn't seem to actually enforce the maximum length for CharFields. In my case, this made the loaddata step fail. I was able to find (and delete) model instances with too long CharField values via:

MyModel.objects.extra(where=["LENGTH(text) > 20"]).delete() 

Once I did this before step 1. above, everything worked.

like image 23
Michael Herrmann Avatar answered Sep 20 '22 02:09

Michael Herrmann