Skip to Content
GuidesMulti-Database

Multi-Database Configuration

Automatic Database Routing Django-CFG provides automatic database routing - no need to manually specify using() on every query. Configure once, and all queries route to the correct database automatically.

Configure multiple databases with automatic routing in Django-CFG.

Basic Multi-Database Setup

Simple Setup (2-3 databases)

Simple Multi-Database Pattern

Perfect for small to medium projects with 2-3 databases.

# config.py import os from django_cfg import DjangoConfig, DatabaseConfig class MyConfig(DjangoConfig): databases: dict[str, DatabaseConfig] = { "default": DatabaseConfig( engine="django.db.backends.postgresql", name="main_db", user="postgres", password=os.environ["DB_PASSWORD"], host="localhost", ), "blog_db": DatabaseConfig( engine="django.db.backends.postgresql", name="blog_db", user="postgres", password=os.environ["DB_PASSWORD"], host="localhost", apps=["blog"], ), }

Simple Pattern Benefits

  • ✅ Easy to understand - Clear database definitions
  • ✅ Type-safe - Full IDE autocomplete
  • ✅ Automatic routing - No manual using() calls
  • ✅ Environment variables - Secure credential management

Advanced Setup (4+ databases)

Advanced Multi-Database Pattern

For large applications with multiple domains and services.

# config.py import os from django_cfg import DjangoConfig, DatabaseConfig class MyConfig(DjangoConfig): databases: dict[str, DatabaseConfig] = { # Core databases "default": DatabaseConfig( engine="django.db.backends.postgresql", name="main", user=os.environ["DB_USER"], password=os.environ["DB_PASSWORD"], host=os.environ["DB_HOST"], conn_max_age=600, # Connection pooling ), # Domain databases "blog_db": DatabaseConfig( engine="django.db.backends.postgresql", name="blog", user=os.environ["DB_USER"], password=os.environ["DB_PASSWORD"], host=os.environ["DB_BLOG_HOST"], conn_max_age=600, ), "shop_db": DatabaseConfig( engine="django.db.backends.postgresql", name="shop", user=os.environ["DB_USER"], password=os.environ["DB_PASSWORD"], host=os.environ["DB_SHOP_HOST"], conn_max_age=600, apps=["shop"], ), # Analytics - read-only replica "analytics": DatabaseConfig( engine="django.db.backends.postgresql", name="analytics", user=os.environ["DB_READONLY_USER"], password=os.environ["DB_READONLY_PASSWORD"], host=os.environ["DB_ANALYTICS_HOST"], options={"connect_timeout": 10, "options": "-c default_transaction_read_only=on"}, apps=["analytics"], ), }

Advanced Pattern Benefits

  • ✅ Scalability - Separate databases per domain
  • ✅ Connection pooling - Better performance
  • ✅ Read replicas - Analytics on separate database
  • ✅ Per-database hosts - Distributed architecture

Microservices Pattern

Microservices Database Pattern

Each service has its own database with shared authentication.

# config.py import os from django_cfg import DjangoConfig, DatabaseConfig class MyConfig(DjangoConfig): databases: dict[str, DatabaseConfig] = { # Shared auth database "default": DatabaseConfig( engine="django.db.backends.postgresql", name="auth_service", user=os.environ["DB_USER"], password=os.environ["DB_PASSWORD"], host=os.environ["DB_AUTH_HOST"], ), # Blog microservice "blog_service": DatabaseConfig( engine="django.db.backends.postgresql", name="blog_service", user=os.environ["DB_USER"], password=os.environ["DB_PASSWORD"], host=os.environ["DB_BLOG_HOST"], apps=["blog"], ), # Shop microservice "shop_service": DatabaseConfig( engine="django.db.backends.postgresql", name="shop_service", user=os.environ["DB_USER"], password=os.environ["DB_PASSWORD"], host=os.environ["DB_SHOP_HOST"], apps=["shop"], ), # Payment microservice "payment_service": DatabaseConfig( engine="django.db.backends.postgresql", name="payment_service", user=os.environ["DB_USER"], password=os.environ["DB_PASSWORD"], host=os.environ["DB_PAYMENT_HOST"], apps=["payments"], ), }

Microservices Considerations Benefits:

  • ✅ Service isolation - Each service has independent database
  • ✅ Independent scaling - Scale databases separately
  • ✅ Technology diversity - Different DB engines per service

Challenges:

  • ⚠️ Cross-service queries - Must use db_constraint=False for FKs
  • ⚠️ Distributed transactions - No ACID across services
  • ⚠️ Data consistency - Eventual consistency patterns needed
  • ⚠️ Testing complexity - Multiple databases to seed

Environment-Based URLs

Keep connection strings in the environment (for example a .env file loaded by your own settings class) and pass them to DatabaseConfig.from_url:

# .env DATABASE_URL=postgresql://user:secret@localhost:5432/main_db BLOG_DATABASE_URL=postgresql://user:secret@localhost:5432/blog_db SHOP_DATABASE_URL=postgresql://user:secret@localhost:5432/shop_db
# config.py import os from django_cfg import DjangoConfig, DatabaseConfig class MyConfig(DjangoConfig): databases: dict[str, DatabaseConfig] = { "default": DatabaseConfig.from_url(os.environ["DATABASE_URL"]), "blog_db": DatabaseConfig.from_url(os.environ["BLOG_DATABASE_URL"], apps=["blog"]), "shop_db": DatabaseConfig.from_url(os.environ["SHOP_DATABASE_URL"], apps=["shop"]), }

URL Format Examples

  • SQLite, relative to the project: sqlite:///db/default.sqlite3
  • SQLite, absolute path: sqlite:////var/lib/app/default.sqlite3
  • PostgreSQL: postgresql://user:[email protected]:5432/main
  • PostgreSQL with SSL: postgresql://user:pass@localhost/main?sslmode=require

Database Routing

Setting apps=[...] on a DatabaseConfig makes Django-CFG install its DatabaseRouter and generate DATABASE_ROUTING_RULES (app label to database alias):

# Generated from apps=[...]; you do not write this yourself DATABASE_ROUTING_RULES = { 'blog': 'blog_db', # All blog app models → blog_db 'shop': 'shop_db', # All shop app models → shop_db # Apps not listed → default database }

Automatic Routing

# models.py - apps/blog/models.py class Post(models.Model): title = models.CharField(max_length=200) # No need to specify database! # Usage - automatically routed to blog_db Post.objects.create(title="Hello") Post.objects.all() # Queries blog_db automatically

Cross-Database ForeignKeys

CRITICAL: db_constraint=False Required When creating ForeignKeys to models in different databases (e.g., User in default, Post in blog_db), you MUST use db_constraint=False. This is required because database constraints cannot span multiple databases.

Implementation

# apps/blog/models.py from django.contrib.auth import get_user_model User = get_user_model() # Lives in 'default' database class Post(models.Model): title = models.CharField(max_length=200) # Cross-database ForeignKey author = models.ForeignKey( User, on_delete=models.CASCADE, related_name='blog_posts', db_constraint=False # ⚠️ REQUIRED for cross-database FK ) class Meta: db_table = 'blog_post'

Why db_constraint=False?

Explanation

Technical reasons:

  • User model is in default database
  • Post model is in blog_db database
  • Database-level foreign key constraints cannot span databases
  • Django ORM handles the relationship at application level
  • Without it: relation "auth_user" does not exist error during migrations

How it works:

  1. Django stores only the foreign key value (author_id=1)
  2. No database constraint created
  3. Relationship resolved at application level when accessed
  4. post.author triggers ORM query to default database

Trade-offs

Benefits:

  • ✅ Cross-database relationships work - Link models across databases
  • ✅ Django ORM validates relationships - post.author, user.blog_posts work
  • ✅ Automatic routing - ORM queries correct database for each model
  • ✅ Select/prefetch works - Can optimize queries with select_related()

Limitations:

  • ❌ No database-level referential integrity - Database won’t enforce constraint
  • ❌ No automatic cascade deletes at DB level - Django handles in Python
  • ❌ Orphaned records possible - If you delete directly in database
  • ❌ Performance overhead - Relationship validation happens in Python

Best Practices

✅ Do:

# Use db_constraint=False for cross-database FKs author = models.ForeignKey( User, on_delete=models.CASCADE, db_constraint=False, # ✅ Required ) # Use Django ORM for all deletes user.delete() # ✅ Cascades to related posts in blog_db

❌ Don’t:

# Don't omit db_constraint=False author = models.ForeignKey(User) # ❌ Will fail during migration # Don't delete directly in database # DELETE FROM auth_user WHERE id=1; # ❌ Orphans posts in blog_db # Don't use database-level cascades # They won't work across databases

Validation strategy:

# Add application-level validation class Post(models.Model): author = models.ForeignKey( User, on_delete=models.CASCADE, db_constraint=False, ) def clean(self): # Validate author exists if not User.objects.filter(id=self.author_id).exists(): raise ValidationError("Author does not exist")

Migration Commands

Django-CFG provides two commands for multi-database migrations:

Automatically migrates all databases:

# Migrate all databases python manage.py migrate_all # Skip automatic makemigrations python manage.py migrate_all --skip-makemigrations

Features:

  • Auto-detects all databases
  • Creates migrations first
  • Migrates each database based on routing rules
  • Handles constance app automatically

Interactive migration tool with auto mode:

# Auto mode - migrates all databases without prompts python manage.py migrator --auto # Interactive mode - shows menu python manage.py migrator # Migrate specific database python manage.py migrator --database blog_db # Migrate specific app python manage.py migrator --app blog

See: Core Commands for detailed migrator documentation.

Complete Production Example

# .env.production DATABASE_URL=postgresql://user:[email protected]:5432/main?sslmode=require&connect_timeout=10 BLOG_DATABASE_URL=postgresql://user:[email protected]:5432/blog?sslmode=require SHOP_DATABASE_URL=postgresql://user:[email protected]:5432/shop?sslmode=require ANALYTICS_DATABASE_URL=postgresql://readonly:[email protected]:5432/analytics?sslmode=require
# config.py import os from django_cfg import DjangoConfig, DatabaseConfig class MyConfig(DjangoConfig): databases: dict[str, DatabaseConfig] = { "default": DatabaseConfig.from_url(os.environ["DATABASE_URL"]), "blog_db": DatabaseConfig.from_url(os.environ["BLOG_DATABASE_URL"], apps=["blog"]), "shop_db": DatabaseConfig.from_url(os.environ["SHOP_DATABASE_URL"], apps=["shop"]), "analytics": DatabaseConfig.from_url(os.environ["ANALYTICS_DATABASE_URL"], apps=["analytics"]), }

Testing Multi-Database Setup

# Create migrations python manage.py makemigrations # Migrate all databases python manage.py migrate_all # Or use migrator in auto mode python manage.py migrator --auto # Test routing in shell python manage.py shell
# Shell - test automatic routing from apps.blog.models import Post from django.contrib.auth import get_user_model User = get_user_model() # Create user in default database user = User.objects.create_user(username='john', email='[email protected]') # Create post in blog_db with cross-database FK post = Post.objects.create( title='Hello World', content='Multi-database setup works!', author=user # Cross-database FK ) # Test relationships print(post.author.username) # 'john' print(user.blog_posts.all()) # QuerySet with post

See Also

Database Configuration

Core Database Setup:

Infrastructure:

Configuration & Setup

Getting Started:

Production:

Examples & Guides

Practical Examples:

Related Guides:

Tools & Operations

CLI & Management:

Background Processing:

Note: All examples use YAML-based configuration via env loader. See Configuration Guide for setup.

Last updated on