← Back to Django Course | Chapter 5: Migrations & Database Management | Lesson 8 of 8

Database Configuration in settings.py

The DATABASES dictionary in settings.py tells Django which database to talk to and how to connect to it.

The Default SQLite Setting

A new Django project's settings.py ships with a DATABASES dictionary already configured for SQLite, using the sqlite3 backend and a file path under BASE_DIR.

Example: The Default SQLite Setting

default is the connection Django uses for every query unless a view explicitly asks for a different one — this is that exact out-of-the-box setting.

markup
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Required Keys for a Database Connection

Every entry in DATABASES needs at minimum an ENGINE (the backend Django should use) and a NAME (the database name, or file path for SQLite) to know where and how to connect.

Example: Required Keys for a Database Connection

ENGINE tells Django which backend module to load; NAME tells it exactly which file (for SQLite) holds the data.

markup
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Checking the Active Connection

You can confirm which database Django is currently configured to use directly from the shell, which is useful when debugging settings changes.

Example: Checking the Active Connection

This prints the ENGINE string currently active, e.g. 'django.db.backends.sqlite3', confirming exactly which backend settings.py is pointing at.

bash
python manage.py shell
>>> from django.conf import settings
>>> settings.DATABASES['default']['ENGINE']

⚠️ Run this command in your terminal.

Common Mistakes
  1. Hard-coding real database passwords directly inside settings.py and committing that file to version control.
  2. Forgetting that changing the DATABASES setting to a different database doesn't move existing data — it just points Django somewhere new and empty.
  3. Misspelling a key inside the default dictionary (like NAME vs DBNAME), which causes Django to fail immediately on startup.
Chapter Summary
  • DATABASES is a dictionary of named connections, and default is the one Django uses unless told otherwise.
  • Each connection needs an ENGINE (which database backend) and connection details like NAME.
  • SQLite only needs a NAME (a file path); other databases also need HOST, PORT, USER, and PASSWORD.
  • Sensitive values like passwords belong in environment variables, not hard-coded in settings.py.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.