Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

python - Changing Django development Database from the default SQLite to PostgreSQL

What are the steps I need to take to migrate from the default SQLite database to Postgres database?

I'm doing this to get my local development environment as close to my live server (which uses postrgres).

Or is there a reason why local development uses SQLite? Is it not recommended to use Postgres for local development?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try the following steps:

1. Install psycopg2 to configure the database:

pip install psycopg2


2. Inside the default settings.py

Change original values:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

To:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'NAME_OF_DB',
        'USER': 'DB_USER_NAME',
        'PASSWORD': 'DB_PASSWORD',
        'HOST': 'localhost',
        'PORT': 'PORT_NUMBER',
    }
}


3. Migrate the DB:

python manage.py makemigrations
python manage.py migrate


EDIT: Thanks @robotHamster comment. Here is the method to sync the existing data:

Backup the data first:

python manage.py dumpdata > datadump.json

After changing the DB setting:

python manage.py loaddata datadump.json


Source: What's the best way to migrate a Django DB from SQLite to MySQL?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...