Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to migrate Django from SQLite to PostgreSQL

How to migrate Django from SQLite to PostgreSQL

Dump existing data:

python3 manage.py dumpdata > datadump.json

Change settings.py to Postgres backend.

Make sure you can connect on PostgreSQL. Then:

python3 manage.py migrate --run-syncdb

Run this on Django shell to exclude contentype data

python3 manage.py shell
>>> from django.contrib.contenttypes.models import ContentType
>>> ContentType.objects.all().delete()
>>> quit()

Finally:

python3 manage.py loaddata datadump.json

Source: https://stackoverflow.com/questions/3034910/whats-the-best-way-to-migrate-a-django-db-from-sqlite-to-mysql

@ronaldlangeveld

This comment has been minimized.

Copy link

@ronaldlangeveld ronaldlangeveld commented Apr 11, 2018

Thank you! Used these steps to migrate my MySQL db to PSQL and it worked perfectly! :D cheers

@gamonsalve

This comment has been minimized.

Copy link

@gamonsalve gamonsalve commented May 6, 2018

It is not working for me, when I run python3 manage.py migrate --run-syncdb, after change settings.py to Postgresql, I get the following Error: django.db.utils.ProgrammingError: relation "etl_categories" does not exist
Am I making a mistake?
SOLVED:
Yes I was making a mistake. Since some views and forms in my app use some Models, when I run "python manage.py migrate" the ERROR above appear. If you are having the same problem just delete or comment all your views, forms and urls that uses any model. Then Follow the steps (migrate, load again your data) and finally uncomment/restore your files.

@bjarnephys

This comment has been minimized.

Copy link

@bjarnephys bjarnephys commented Jul 11, 2018

tables created, ok, but data were not inserted!

because datadump.json have no data user data

@Lapicher

This comment has been minimized.

Copy link

@Lapicher Lapicher commented Jul 17, 2018

Thankssssss so much!!! this steps works to me too. To migrate from PostgreSQL to MSSQL Server. Works perfectly

@abitran

This comment has been minimized.

Copy link

@abitran abitran commented Jul 18, 2018

This method didn't work for me. I encountered a lot of errors. Also loaddata will consume a lot of memory. What worked for me was to run sequel from ruby. Just run the commands:
gem install sequel
You will need to have installed on your system the devel packages for postgres , sqlite and ruby
Run the command:
gem install pg sqlite3
Create an empty database on postgresql, let's say testDB and assign a grant permission to a user
From the command prompt run:
sequel -C sqlite:///path/to/sqlitedb.db postgres://user:password@host/testDB
This will run with no errors.
Change the settings in your django project to work with the postgres database
Run
./manage migrate (not necessary)
Run the server

@gabrielponto

This comment has been minimized.

Copy link

@gabrielponto gabrielponto commented Aug 18, 2018

To not found error when use loaddata to load fixture, exclude contenttype and auth.Permission modules. So the dump line will be:

python manage.py dumpdata --exclude=contenttypes --exclude=auth.Permission > datadump.json

I found this help on: https://coderwall.com/p/kogbla/django-fixture-dumpdata-loaddata-and-integrity-error

@AhtishamChishti

This comment has been minimized.

Copy link

@AhtishamChishti AhtishamChishti commented Aug 29, 2018

Guys I am getting django.db.utils.IntegrityError can anyone please help.

@bnelson365

This comment has been minimized.

Copy link

@bnelson365 bnelson365 commented Dec 9, 2018

Thank you @gabriel, that worked for me

@mlazowik

This comment has been minimized.

Copy link

@mlazowik mlazowik commented Jan 9, 2019

In case anyone runs into issues like object has no attribute '_something_cache': if you're using custom pre_save or post_save handlers you need to handle the raw=True case in them: https://stackoverflow.com/a/19600001 https://docs.djangoproject.com/en/2.0/ref/django-admin/#what-s-a-fixture

@aniagarwal97

This comment has been minimized.

Copy link

@aniagarwal97 aniagarwal97 commented Apr 17, 2019

Not working for me. I am getting this error

django.db.models.fields.related_descriptors.RelatedObjectDoesNotExist: Problem installing fixture '/home/ubuntu/news-manifest-master/datadump.json': Comment has no content_type.

@mbigatimothy

This comment has been minimized.

Copy link

@mbigatimothy mbigatimothy commented May 15, 2019

Thanks a bunch. I followed this and it worked perfectly

@omidekz

This comment has been minimized.

Copy link

@omidekz omidekz commented Aug 12, 2019

Thank YOU SO MUUUUUUUCH

@WilliamHH

This comment has been minimized.

Copy link

@WilliamHH WilliamHH commented Sep 1, 2019

Indeed thank you for this post!

@AlanJui

This comment has been minimized.

Copy link

@AlanJui AlanJui commented Sep 28, 2019

😍🤩🥳👍

👏👏👏👏👏........................

@adsk2050

This comment has been minimized.

Copy link

@adsk2050 adsk2050 commented Feb 9, 2020

Hello @sirodoht ! I have a question. If you remove ContentTypes then what else remains in the database?
I have created many pages in my website in db.sqlite3 database itself and I don't want to recreate everything again. So I need to include ContentTypes in the migration which is causing problems for me and returning ProgrammingError.

@ronaldlangeveld

This comment has been minimized.

Copy link

@ronaldlangeveld ronaldlangeveld commented Mar 2, 2020

Hello @sirodoht ! I have a question. If you remove ContentTypes then what else remains in the database?
I have created many pages in my website in db.sqlite3 database itself and I don't want to recreate everything again. So I need to include ContentTypes in the migration which is causing problems for me and returning ProgrammingError.

You will add the backed up data after removing contenttypes.. so it won't be lost.

@anefta

This comment has been minimized.

Copy link

@anefta anefta commented Dec 15, 2020

Guys I am getting django.db.utils.IntegrityError can anyone please help.

Hey, @AhtishamChishti I am getting also this error..Any solution to this?

@aishwarya2202

This comment has been minimized.

Copy link

@aishwarya2202 aishwarya2202 commented Mar 19, 2021

it is asking for login to enter in project.
could plz tell me its username and passward?

@amiaynara

This comment has been minimized.

Copy link

@amiaynara amiaynara commented Apr 8, 2021

tables created, ok, but data were not inserted!

because datadump.json have no data user data

How did you resolve the issue. I carried out the instructions successfully, and it showed [Installed 156 objects from 1 fixture]. But there is no data when i see in the web-app or in the pgadmin4. Any help is appreciated.

@luizfelipevbll

This comment has been minimized.

Copy link

@luizfelipevbll luizfelipevbll commented May 3, 2021

image

Error migrating from MySQL to MSSQL

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