Django

Django database connection settings

django databases connection with sqlite | mysql | postgresql | mongo | oracle.png

Django has the build in support for most of the available databases here is the list of most common databases and how to connect them to your django project.

starting with the standard which comes on the initial creation of project.

Django SQLite Settings

This SQLite connection comes by default when a new project is created. It is a lightweight library which allows most of the relational database features but with minimal setup you just specify a file name like in the example below db.sqlite3 will be the file name and it will be generated at the BASE_DIR path which is by default the root location of your project i.e. beside the manage.py file. This db.sqlite3 will contain all of your database structure as well as data deleting this file will delete essentially the database upon restarting the server however it will create the file again but there wont be any data in it not even the migrations structure etc.
NOTE: This is highly not recommended for any production environment for obvious reasons. ADD to .gitignore and other ignore configs if you are using some other.

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

Django PostgreSQL Settings

To connect your django project to postgreSQL database you will need couple of things

  1. install psycopg2 package using pip
  2. your postgreSQL database is fully setup on local or server which means you have these settings available (HERE IS HOW TO DO THAT)
    1. Database Name
    2. postgres user who has rights to that database
    3. host (In case of local setup its localhost)
    4. port (default is 5432)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db_name',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'localhost',
'PORT': '', # default is 5432
}
}

Django MySQL Settings

To setup your complete MySQL environment follow this tutorial
MySQL DB setup with Django
Since I am focusing only on the Database connections so, here are the connection settings.

# settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': '/path/to/my.cnf',
},
}
}

# my.cnf
[client]
database = NAME
user = USER
password = PASSWORD
default-character-set = utf8

Django Oracle Settings

More info on how to setup your complete Oracle environment visit
Setup Oracle with Django

The connection string however should look like this.

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.oracle',
'NAME': 'xe',
'USER': 'a_user',
'PASSWORD': 'a_password',
'HOST': '',
'PORT': '',
}
}

Django MongoDB Settings

Django can also be setup with nosql databases like MongoDB here is complete guide on how to do that
MongoDB setup for Django
The Connection string will be

#For Local Enviroment
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'database_name',
}
}

However if its somewhere on server like mlab etc the connection string should be

DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'database_name',
'HOST': 'mongodb://<database_user>:<database_password>@ds414214.mlab.com:59144/<database_name>',
'USER' : 'database_user',
'PASSWORD' : 'database_password',
}
}


About author

shahraiz ali.jpeg

Shahraiz Ali

I'm a passionate software developer and researcher from Pakistan. I like to write about Python, Django and Web Development in general.



Scroll to Top