Django Database Configurations

Listen as audio
Django Database Settings
Image by IO-Images from Pixabay

Hello Techies,

We know that Django Database configurations come with a default Django sqlite settings but what if you want to use a different database in your project? Don’t worry friends, in this blog I will tell you how to configure 3 different databases with examples in Django settings.

In this blog, I’m covering MySql, Mssql, and PostgreSQL Django Database configurations.

Below is the default Django Sqlite settings.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

Django Database Configurations of Different Databases

You need to modify the settings.py file for the Django database configuration. Django needs some basic information about the database, such as what the engine is being used, the name of the database, the password, the username, the host. Follow the steps below to configure these settings.

Django Database Configurations for the Mssql Database

Below are the parameters of the MSSQL database. MSSQL database requires driver name also so in the OPTIONS parameter add the driver’s name. You can change the driver name according to your MSSQL driver.

DATABASES = {
   'default': {
       'ENGINE': 'sql_server.pyodbc', # engine
       'NAME': 'db_name', #mssql  database name
       'HOST': '127.0.0.1', # mssql database host
       'USER': 'db_username', # mssql database username
       'PASSWORD': 'db_password', # mssql database password
       'PORT': 1433, # mssql database port
       'OPTIONS': {'driver': 'ODBC Driver 13 for SQL Server'} # odbc drivers
   }
}

If the above configuration for MSSQL doesn’t work for you then change USER to USERNAME. Check the below example.

DATABASES = {
   'default': {
       'ENGINE': 'sql_server.pyodbc', # engine
       'NAME': 'db_name', #mssql  database name
       'HOST': '127.0.0.1', # mssql database host
       'USERNAME': 'db_username', # mssql database username
       'PASSWORD': 'db_password', # mssql database password
       'PORT': 1433, # mssql database port
       'OPTIONS': {'driver': 'ODBC Driver 13 for SQL Server'} # odbc drivers
   }
}

Django Database Configurations for the Mysql Database

Below are the parameters for the Django Mysql database.

DATABASES = {
   'default': {
       'NAME': 'db_name', # mysql database name
       'ENGINE': 'django.db.backends.mysql', # engine
       'HOST': '127.0.0.1', # mysql database host
       'USER': 'db_username', # mysql database user
       'PASSWORD': 'db_password', # mysql database password
       'PORT': 3306, # mysql database port
   }
}

Django Database Configurations for the PostgreSQL Database

Below are the parameters for the PostgreSQL database.

DATABASES = {
   'default': {
       'NAME': 'db_name', #database name
       'ENGINE': 'django.db.backends.postgresql_psycopg2', # engine
       'HOST': '127.0.0.1', # postgresql database host
       'USER': 'db_usernam', # postgresql database username
       'PASSWORD': 'db_password', # postgresql database password
       'PORT': '5432', # postgresql database port
   }
}

Hope you all clear with Database configurations and still if you are facing any problem please feel free to contact us or you can comment below.

Leave a Comment