Skip to main content

django-db-tenants

CI

Database-per-client multi-tenancy for Django.

  • One shared database holds the client registry. Every client gets its own database with its own auth users, sessions and application tables.
  • Tenant databases are registered in django.db.connections at runtime, so adding, suspending or re-pointing a client never requires a restart.
  • How a client's database is reached is decided by its database strategy, configured by encrypted per-client parameters. The built-in one derives tenant connections from the shared database's settings; write your own for anything else.

Documentation: the developer's guide is at https://gigiusb.github.io/django-db-tenants/ (sources in docs/).

Install

pip install django-db-tenants
pip install "django-db-tenants[postgres]"  # extras: postgres, mysql

Requires Python 3.11+ and Django 4.2, 5.2, 6.0 or 6.1.

Configure

# settings.py
INSTALLED_APPS = [
    "django_db_tenants",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    ...,
]

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django_db_tenants.middleware.TenantMiddleware",  # before sessions + auth
    "django.contrib.sessions.middleware.SessionMiddleware",
    ...,
]

DATABASE_ROUTERS = ["django_db_tenants.router.TenantRouter"]

DATABASES = {
    # The shared database (client registry), also the starting point of tenant connections.
    # Its account needs the right to create databases to provision tenants.
    "default": {"ENGINE": "django.db.backends.postgresql", "NAME": "control", "USER": "app", ...},
}

DB_TENANTS = {
    "CONTROL_HOSTS": ["admin.yourapp.com"],  # served from the shared database
    "ENCRYPTION_KEY": env("ENCRYPTION_KEY"),  # Fernet key for the stored client secrets
}
python manage.py migrate  # the shared database

Use

Add a client: this creates its database, migrates it and serves acme.yourapp.com from it, without a restart.

python manage.py provision_tenant acme --name "ACME S.p.A." --domain acme.yourapp.com --db-name tenant_acme

Requests are routed to the tenant of their host. Outside a request, activate a tenant yourself:

from django_db_tenants import tenant_context
from django_db_tenants.tasks import with_tenant

with tenant_context("acme"):
    Invoice.objects.count()  # runs on tenant_acme


@with_tenant
def send_invoices(tenant): ...  # background jobs: pass the slug or the Client
python manage.py migrate_tenants  # migrate every active tenant database

Users are ordinary Django users in each tenant database, so any Django authentication works.

Next: the architecture, database strategies, settings and cache isolation. The demo project in tests/demo shows a complete setup.

Contributing

See CONTRIBUTING.md to set up the development environment and run the tests.

License

django-db-tenants is source-available under the PolyForm Noncommercial License 1.0.0:

  • Noncommercial use is free: personal study, research, hobby projects, and use by charities, educational institutions, public research organizations and government institutions.
  • Commercial use needs a commercial license, including a company using the library in its products, services or internal systems. Write to Giovanni Bronzini at g.bronzini@gmail.com.

Versions up to 0.2.0 were released under the MIT license.

Metadata

Release files for django-db-tenants 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-db-tenants 0.3.0
File Size Uploaded Provenance Actions
django_db_tenants-0.3.0.tar.gz 30.2 kB With provenance Details for django_db_tenants-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-db-tenants 0.3.0
File Interpreter ABI Platform Actions
django_db_tenants-0.3.0-py3-none-any.whl Python 3 none any Details for django_db_tenants-0.3.0-py3-none-any.whl

Total release size: 58.4 kB

Release files / django_db_tenants-0.3.0.tar.gz

Download URL django_db_tenants-0.3.0.tar.gz
Size 30.2 kB
Tags Source
SHA-256 checksum
How to use checksums
381d01f4d98ff4e36e3ace0d2e3d4693ce3dd84c15788f47510e6cec50a2e8b9
BLAKE2b-256 checksum
How to use checksums
17c70a50b9ef8c1d865430dcc21cbabfbf0800cc29358c09b0965b736e20d0d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.

Transparency log

Release files / django_db_tenants-0.3.0-py3-none-any.whl

Download URL django_db_tenants-0.3.0-py3-none-any.whl
Size 28.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ad338fd170de17e91f87e516bf12207412f6b8ce4d3dc59f4ed78c937b19d011
BLAKE2b-256 checksum
How to use checksums
23b1b5cc1e49e5c4c34cd3decea35b4cd059f783a8b3d415e783b7cefbb271ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.

Transparency log

Security for django-db-tenants 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for django_db_tenants-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page