Database Setup
This guide will help you set up the database configurations for your Django project, covering both local testing using SQLite and deploying to production using PostgreSQL.
SQLite - Local Testing
SQLite is the default database used by Django for local development. It's a lightweight disk-based database that doesn't require a separate server process and is ideal for development and testing. Below is the default configuration for SQLite in your Django settings.py
file:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}
This configuration specifies that Django uses the SQLite database, with the database file (db.sqlite3) located in the base directory of your project.
PostgreSQL - Production Environment
For production environments, PostgreSQL is a popular choice due to its robustness, scalability, and support for advanced features. Below is a template configuration for PostgreSQL in your Django settings.py file:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "DB_NAME",
"USER": "DB_USERNAME",
"PASSWORD": "DB_PASS",
"HOST": "DB_HOST",
"PORT": "DB_PORT",
}
}
Replace DB_NAME, DB_USERNAME, DB_PASS, DB_HOST, and DB_PORT with your actual database name, username, password, host, and port respectively.
Provisioning a PostgreSQL Database
You can provision a PostgreSQL database from various cloud providers like Digital Ocean, AWS, or GCP. Note: your database server should be physically located close to your webserver, preferably in the same data center region.