← Back to Chapters

Multi-Database Setup in Django

?️ Multi-Database Setup in Django

? Quick Overview

Django supports working with multiple databases in a single project. This is useful for separating read/write databases, integrating legacy databases, or handling data from various sources. This guide explains how to configure and manage multiple databases in Django.

? Key Concepts

  • Multi-Database Architecture: One Django project connected to multiple databases.
  • Database Routers: Control where read/write operations are executed.
  • Explicit Queries: Using using() to target a specific database.

? Syntax & Theory

  • Django uses the DATABASES setting to define multiple database connections.
  • Custom routers decide how Django routes queries and migrations.
  • Migrations must be applied individually per database.

? Code Examples

? View Code Example
# settings.py - defining multiple databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'primary_db',
'USER': 'user',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '5432',
},
'secondary': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'secondary_db',
'USER': 'user',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '5433',
}
}
? View Code Example
# routers.py - routing reads and writes
class PrimaryDatabaseRouter:
def db_for_read(self, model, **hints):
return 'default'
def db_for_write(self, model, **hints):
return 'default'
def allow_relation(self, obj1, obj2, **hints):
return True
def allow_migrate(self, db, app_label, model_name=None, **hints):
return db == 'default'

class SecondaryDatabaseRouter:
def db_for_read(self, model, **hints):
return 'secondary'
def db_for_write(self, model, **hints):
return 'secondary'
def allow_relation(self, obj1, obj2, **hints):
return True
def allow_migrate(self, db, app_label, model_name=None, **hints):
return db == 'secondary'
? View Code Example
# views.py - querying specific databases
from django.shortcuts import render
from myapp.models import Model1, Model2

def view_function(request):
primary_data = Model1.objects.using('default').all()
secondary_data = Model2.objects.using('secondary').all()
return render(request,'template.html',{
'primary_data': primary_data,
'secondary_data': secondary_data
})

? Live Output / Explanation

  • Primary database handles default reads and writes.
  • Secondary database processes separate queries independently.
  • Each database operates without interfering with the other.

? Interactive Architecture Diagram

# Conceptual flow diagram
User Request
↓
Django ORM
↓
Database Router
↙           ↘
Primary DB   Secondary DB

?️ Database Router Simulator

Click a Django operation to see how the Router directs traffic to the correct database.

?️ Default Port 5432
⚡ Router ⚡
?️ Secondary Port 5433
Ready to route...

?️ Use Cases

  • Read-heavy applications using replica databases.
  • Connecting legacy databases alongside new systems.
  • Microservice-style data separation.

✅ Tips & Best Practices

  • Always test routers thoroughly before production.
  • Use explicit using() calls for critical queries.
  • Monitor database performance continuously.
  • Apply migrations carefully per database.

? Try It Yourself

  • Configure two SQLite databases and test routing.
  • Create a router for a specific app only.
  • Run migrations separately using the --database flag.