Introduction
This article is another article which is containing tip on how to define multi database configuration in Django application. As an additional reference, this article has a similar content with the previous article. That article is in How to Define Database Configuration in Django Application. Basically, in that article, there is only one database definition in the Django project setting file. Actually, Django project or Django application itself are supporting to use multi database. In this context, the term of multi database actually expand through out different types of databases. So, there can be a connection from the Django project or Django application to MySQL database, PostgreSQL database and also Microsoft SQL Server and even other database types such as Oracle Database. The most important thing is that there is a Django library exist for supporting the database connection process.
How to Define Multi Database Configuration in Django Application
So, just access the file for defining database connection configuration of the Django project. It exist in a file with the name of ‘settings.py’. Actually, below is an example of the Django project structure :
C:\django\myproject>tree Folder PATH listing for volume Windows-SSD Volume serial number is CA30-19A4 C:. ├───myapp │ ├───migrations │ │ └───__pycache__ │ ├───templates │ │ └───myapp │ └───__pycache__ └───myproject └───__pycache__ C:\django\myproject>
For more information about how to create the Django project and also the Django application inside of it, read How to Create Django Project in Microsoft Windows using Command Line for creating a Django project. Another one is about How to Create a Django Application inside a Django Project in Microsoft Windows.
So, that file for defining database definition exist in the ‘myproject’ folder which is the folder of the Django project as follows :
C:\django\myproject\myproject>tree /F . Folder PATH listing for volume Windows-SSD Volume serial number is CA30-19A4 C:\DJANGO\MYPROJECT\MYPROJECT │ asgi.py │ settings.py │ urls.py │ wsgi.py │ __init__.py │ └───__pycache__ settings.cpython-310.pyc urls.cpython-310.pyc wsgi.cpython-310.pyc __init__.cpython-310.pyc C:\django\myproject\myproject>
Open the ‘settings.py’ file and search for the following part :
# Database # https://docs.djangoproject.com/en/4.0/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } }
So, aside from the ‘default’ database which may have already changed into another type of database, just add a new database beside the ‘default one’ as follows :
# Database # https://docs.djangoproject.com/en/4.0/ref/settings/#databases DATABASES = { # 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME': BASE_DIR / 'db.sqlite3', # } 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'django_training', 'USER': 'postgres', 'PASSWORD': 'password', 'HOST': 'localhost', 'PORT': '5432' }, 'myapp' : { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'db_myapp', 'USER': 'postgres', 'PASSWORD': 'password', 'HOST': 'localhost', 'PORT': '5432' } }
As in the above revision, there is one new database definition configuration with the name of ‘myapp’.