How to change default SQLite database in Django project

By default, the Django project’s configuration uses SQLite database. If you’re new to databases, or you’re just interested in trying Django, this is the easiest choice. SQLite is included in Python, so you won’t need to install anything else to support your database.

If we open “your_django_project/settings.py” , you will see following “DATABASES” setting with default ENGINE and NAME. The DATABASES setting must configure a default database

[bash] # Database # https://docs.djangoproject.com/en/3.0/ref/settings/#databases DATABASES = { ‘default’: { ‘ENGINE’: ‘django.db.backends.sqlite3’, ‘NAME’: os.path.join(BASE_DIR, ‘db.sqlite3’), } } [/bash]

If we want to change the database type from SQLite to anything else such as Postgresql, mySQL etc, then we need to change the parameters in above settings.py to something like below, [ Below example is for postgresql database ]

[bash] DATABASES = { ‘default’: { ‘ENGINE’: ‘django.db.backends.postgresql’, ‘NAME’: ‘mydatabase’, ‘USER’: ‘mydatabaseuser’, ‘PASSWORD’: ‘mypassword’, ‘HOST’: ‘127.0.0.1’, ‘PORT’: ‘5432’, } } [/bash]

The description for settings parameters used above are as,

ENGINE – Either ‘django.db.backends.sqlite3’, ‘django.db.backends.postgresql’, ‘django.db.backends.mysql’, or ‘django.db.backends.oracle’

NAME – The name of your database. If you’re using SQLite, the database will be a file on your computer; in that case, NAME should be the full absolute path, including filename, of that file. The default value, os.path.join(BASE_DIR, ‘db.sqlite3’), will store the file in your project directory. For SQLite, it’s the full path to the database file. When specifying the path, always use forward slashes, even on Windows (e.g. C:/homes/user/mysite/sqlite3.db).

Reference : https://docs.djangoproject.com/en/3.0/ref/settings/#std:setting-DATABASES

Leave a Comment