Database configuration tells Django where and how to save your app's data. It connects your app to a storage place.
Database configuration in Django
Introduction
Syntax
Django
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}ENGINE tells Django which database type to use.
NAME is the database file or database name.
Examples
Django
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'myuser',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '5432',
}
}Django
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb',
'USER': 'root',
'PASSWORD': 'secret',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}Django
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'mydatabase.sqlite3',
}
}Sample Program
This example sets up a simple SQLite database configuration and prints the engine and file path.
Django
from pathlib import Path BASE_DIR = Path(__file__).resolve().parent.parent DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } } print(f"Database engine: {DATABASES['default']['ENGINE']}") print(f"Database file: {DATABASES['default']['NAME']}")
Important Notes
Use BASE_DIR to keep database files inside your project folder.
For production, avoid using SQLite; use PostgreSQL or MySQL instead.
Keep your database passwords safe and do not hardcode them in public code.
Summary
Database configuration connects your Django app to a data storage.
Set ENGINE and NAME to tell Django which database to use.
Use different settings for development (SQLite) and production (PostgreSQL/MySQL).
Practice
1. What is the purpose of the
ENGINE setting in Django's database configuration?easy
Solution
Step 1: Understand the role of
TheENGINEENGINEsetting tells Django which database backend to use, like SQLite, PostgreSQL, or MySQL.Step 2: Differentiate from other settings
Settings likeNAME,USER, andPASSWORDhandle connection details, not the database type.Final Answer:
To specify the type of database Django should use -> Option CQuick Check:
ENGINE = database type [OK]
Hint: ENGINE always means database type, not credentials [OK]
Common Mistakes:
- Confusing ENGINE with NAME or USER
- Thinking ENGINE sets username or password
- Assuming ENGINE is a file path
2. Which of the following is the correct way to configure a SQLite database in Django's
settings.py?easy
Solution
Step 1: Identify correct ENGINE for SQLite
Django usesdjango.db.backends.sqlite3as the ENGINE for SQLite databases.Step 2: Check the NAME setting for SQLite
The NAME should be a path to the SQLite file, commonlyBASE_DIR / 'db.sqlite3'using pathlib.Final Answer:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3' } } -> Option DQuick Check:
SQLite ENGINE = django.db.backends.sqlite3 [OK]
Hint: SQLite uses django.db.backends.sqlite3 and file path NAME [OK]
Common Mistakes:
- Using 'sqlite3' instead of 'django.db.backends.sqlite3'
- Confusing ENGINE with PostgreSQL or MySQL backends
- Setting NAME as just a string without path
3. Given this Django database configuration snippet, what database will Django connect to?
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'myproject',
'USER': 'admin',
'PASSWORD': 'secret',
'HOST': 'localhost',
'PORT': '5432',
}
}medium
Solution
Step 1: Check the ENGINE setting
The ENGINE is set todjango.db.backends.postgresql, which means Django will use PostgreSQL.Step 2: Identify database details
The NAME is 'myproject', HOST is 'localhost', and PORT is '5432' which are typical PostgreSQL defaults.Final Answer:
PostgreSQL database named 'myproject' on localhost -> Option AQuick Check:
ENGINE = postgresql means PostgreSQL DB [OK]
Hint: ENGINE shows database type; PostgreSQL means option C [OK]
Common Mistakes:
- Assuming SQLite because of 'myproject' name
- Confusing PostgreSQL with MySQL
- Ignoring HOST and PORT settings
4. Identify the error in this Django database configuration:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb',
'USER': 'root',
'PASSWORD': 'pass123',
'HOST': '',
'PORT': '',
}
}medium
Solution
Step 1: Check HOST and PORT values
HOST and PORT are empty strings, which can cause connection issues because MySQL expects valid host and port or defaults.Step 2: Understand default behavior
Leaving HOST and PORT empty may work if MySQL uses defaults, but empty strings are not recommended and can cause errors.Final Answer:
Empty HOST and PORT cause connection failure -> Option BQuick Check:
HOST and PORT must be valid or omitted [OK]
Hint: Empty strings for HOST/PORT can break MySQL connection [OK]
Common Mistakes:
- Thinking empty strings mean localhost defaults
- Confusing ENGINE with PostgreSQL
- Assuming PASSWORD must be non-empty always
5. You want to configure Django to use PostgreSQL in production but SQLite in development. Which approach correctly handles this in
settings.py?hard
Solution
Step 1: Understand environment-based configuration
Using environment variables lets you switch database settings without changing code, ideal for dev vs prod.Step 2: Why other options fail
Always setting PostgreSQL ENGINE won't work in dev without PostgreSQL installed; changing USER alone doesn't switch DB type; Django does not auto-pick between multiple databases without explicit routing.Final Answer:
Use environment variables to switch ENGINE and NAME based on environment -> Option AQuick Check:
Env vars enable flexible DB config [OK]
Hint: Use env vars to switch DB settings per environment [OK]
Common Mistakes:
- Hardcoding production DB in dev settings
- Assuming Django auto-selects DB from multiple configs
- Changing USER does not switch database type
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
