Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 582
Zorgan Avatar asked May 14 '18 04:05

Zorgan


1 Answers

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?
like image 97
R.yan Avatar answered Oct 06 '22 22:10

R.yan