简体   繁体   中英

How to migrate from sqlite to postgres using a local sqlite database in Django?

I have a 'xxxxx.db' sqlite3 database and I want to move the data to PostgreSQL. I have done some research and seen a couple of options but none of them worked (PGloader etc.). What are some other options? I am using Windows but solutions in Linux are welcome as well.

I have tried doing it in PowerShell(via Jupyter Notebook):

!pip install virtualenvwrapper-win
!mkvirtualenv [name]
!pip install django
!django-admin startproject [name]
cd [name]
!python manage.py startapp app
!python manage.py inspectdb
!python manage.py inspectdb > models.py
!python manage.py migrate
!manage.py runserver
!python manage.py dumpdata > data.json

But the dump does not include the data from my db, I have also changed the settings to

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': absolute_path_to_db/db,
    }
} 

Thanks in advance!

The dumpdata command [Django-doc] will dump data of the activated database. Since you are pointing to the new database, that means the database is (likely) empty, and thus there is no data to dump at all.

You only need to activate the virtual environment and dump the data, so with the old database as active database in the settings.py , you activate the virtual environment, and dump the data in a JSON file:

. virtualenv/bin/activate
python3 manage.py  --all > data.json

with virtualenv the name of the directory where the virtual environment is stored.

Next in your settings, you can rewrite this to use the (new) PostgreSQL database, for example with:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': '<db-name>',
        'USER': '<db-user>',
        'PASSWORD': '<db-password>',
        'HOST': '<db-host>',
        'PORT': '5432',
    }
}

You should install the psycopg2 package [PyPi] , migrate the database to create the tables, and then you can load the data from the data.json with the loaddata command [Django-doc] :

. virtualenv/bin/activate
pip3 install psycopg2
python3 manage.py 
python3 manage.py  data.json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM