Database Setup (SQLite & PostgreSQL)
Django supports multiple databases — SQLite (default, file-based), PostgreSQL (production-grade), MySQL, and Oracle. SQLite is perfect for development; PostgreSQL is the recommended choice for production.
15 min•By Priygop Team•Updated 2026
Database Backends
- SQLite — Default, zero config, file-based, great for development
- PostgreSQL — Recommended for production, supports JSONField, full-text search
- MySQL — Popular alternative, widely supported
- Oracle — Enterprise option
- Switch databases by changing DATABASES in settings.py
- Install database adapter: pip install psycopg2-binary (PostgreSQL)
- Django ORM abstracts SQL differences — same code works on any database
Database Configuration
Database Configuration
# settings.py
# SQLite (default — development)
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
# }
# }
# PostgreSQL (production)
# pip install psycopg2-binary
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.postgresql',
# 'NAME': 'mydb',
# 'USER': 'myuser',
# 'PASSWORD': 'mypassword',
# 'HOST': 'localhost',
# 'PORT': '5432',
# }
# }
# Using environment variables (recommended for production)
# import os
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.postgresql',
# 'NAME': os.environ.get('DB_NAME', 'mydb'),
# 'USER': os.environ.get('DB_USER', 'myuser'),
# 'PASSWORD': os.environ.get('DB_PASSWORD', ''),
# 'HOST': os.environ.get('DB_HOST', 'localhost'),
# 'PORT': os.environ.get('DB_PORT', '5432'),
# }
# }Tip
Tip
Review migration files before applying. Use python manage.py sqlmigrate appname 0001 to see the SQL that will run.
Diagram
Loading diagram…
QuerySets are LAZY — no DB hit until evaluated.
Common Mistake
Warning
Deleting migration files to fix issues. This corrupts migration history. Use squashmigrations or data migrations instead.
Practice Task
Note
(1) Make a model change and create a migration. (2) Review the SQL with sqlmigrate. (3) Apply with migrate.
Quick Quiz
Key Takeaways
- Django supports multiple databases — SQLite (default, file-based), PostgreSQL (production-grade), MySQL, and Oracle.
- SQLite — Default, zero config, file-based, great for development
- PostgreSQL — Recommended for production, supports JSONField, full-text search
- MySQL — Popular alternative, widely supported