When working with large organizations or multi-branch systems, it's common to maintain multiple databases that store similar structured data. Instead of running separate Django projects for each database, we can run a single Django application and switch the database based on the port used in the request.
This guide explains how to configure port-wise database routing in Django using SQL Server and the mssql-django backend.
| Scenario | Benefit |
|---|---|
| Multi-branch system (Branch A, Branch B, etc.) | Each branch works on its own database |
| Shared application logic | One codebase, many databases |
| Easy deployment & maintenance | Update once → effects all branches |
| Simple request routing | Port decides which DB is active |
Example:
| Port | Selected Database | Example URL |
|---|---|---|
8001 |
DB_MAIN_2024 |
http://server:8001/ |
8002 |
DB_BRANCH_2025 |
http://server:8002/ |
This ensures each request uses only the database belonging to its port.
Suppose you need a 3rd database DB_CITY_2026 on port 8003.
settings.pymiddleware.pyDone.
| ✅ Item | Why |
|---|---|
| Database exists | Must be created first |
| Login credentials correct | Avoid auth failure |
| SQL Server port 1433 open | Networking requirement |
| ODBC Driver installed | Required backend driver |
| DB entry added in settings.py | For Django to see it |
| Port mapping added in middleware | For routing to work |
| App restarted | Config must reload |
This architecture allows:
One Django codebase
Multiple databases
Clean port-based switching
Easy scaling for future branches/clients/cities
Get the latest news and updates by signing up to our daily newsletter.We won't sell your email or spam you !