Switch to a new Database in Django

It is easier to work with the default SQLite database when you get started in Django. But you have to change the database afterward when you are going to put your project in production. This tutorial describes the step by step process required to switch the database of your Django project. You can also migrate the data from your old database to the new one by following this tutorial.

Note: Do not delete the migrations folder.

Add the new Database to settings.py

Edit your settings.py and add the new database to the DATABASES list and give it the name “new”. For example, I am using the MySQL database, so I will add the following code:

    'new': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'databasename',
        'USER': 'databaseusername',
        'PASSWORD': 'databasepassword',
        'HOST': 'localhost',
        'PORT': '3306',
    }

After adding the above snippet, my DATABASES look like this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    'new': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'tFnXEEpcjQ',
        'USER': 'tFnXEEpcjQ',
        'PASSWORD': 'UW9BWCT8m2',
        'HOST': 'remotemysql.com',
        'PORT': '3306',
    }
}

I have used the MySQL database in this example. You will add the code for the database that you are using. See the instructions for setting up a new database here: https://docs.djangoproject.com/en/3.0/ref/databases/ . If you want to get more information about using multiple databases in Django, check this: https://docs.djangoproject.com/en/3.0/topics/db/multi-db/

Note: You need to install the client for whatever database you are using on your machine.

Create tables in the new Database

Make sure that you have not deleted the migrations folder. If you have deleted it by accident, please make migrations again.

To create tables in your new database, run the migrations on it using the following command:

 python manage.py migrate --database=new

Note: Here ‘new’ is the name of the database we defined in the previous step.

Transfer data to the new Database

You can skip this step if you do not want to transfer the data from your old database to the new one. First, clear the new database by running the following command:

python manage.py flush --database=new

Export data from the old Database

Export data from your current database to a JSON file using the following command:

python manage.py dumpdata>data.json

Load Data into the new Database

Now load data into the new database using the following command:

python manage.py loaddata data.json --database=new

Remove the old Database

Now remove the old database from settings.py and rename the new database to default. My final DATABASES look like this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'tFnXEEpcjQ',
        'USER': 'tFnXEEpcjQ',
        'PASSWORD': 'UW9BWCT8m2',
        'HOST': 'remotemysql.com',
        'PORT': '3306',
    }
}

You have successfully switched your database in Django. If you have any questions or confusion, you can ask in the comments and I will provide the answer in the FAQ section here.

1 thought on “Switch to a new Database in Django”

  1. Thank you for this wonderful post, it was really helpful. But i still have some errors, certain tables are not been migrated.

    app.models.DoesNotExist: rMetadataform matching query does not exist.

    any suggestions

    Reply

Leave a Comment