Jan. 18, 2023

Change the database from SQLite to MySQL in Django


More on:   
Go back

Post Image
The default database shipped with Django is SQLite. It is meant to be used on tests only and not in production. Changing SQLite to MySQL takes the following steps:


  • First create a JSON type file with the data that is currently in the SQLite database. To do that, enter the following command on the bash terminal
  • python manage.py dumpdata > name_of_file.json

  • Edit the settings.py file in Django to connect to the MySQL database.

  • Then run the migrate command to set up the database. Notice the double dash before run.
  • python manage.py migrate —run-syncdb

    ContentType.objects is a model created by Django that has information of the models you created. It needs to be removed. You need to do it with the Django’s shell.
  • Start Django’s shell with this command:
  • python manage.py shell

  • Then import the module:
  • from django.contrib.contenttypes.models import ContentType

  • Delete the ContentType objects:
  • ContentType.objects.all().delete()

  • Exit the shell:
  • quit()

  • Run loaddata as follows, still in the bash, to get the data stored in the JSON file created on the first step.
  • python manage.py loaddate name_of_file.json