Django Tenant Schemas

Tenant support for Django using PostgreSQL schemas.
Alternatives To Django Tenant Schemas
Project NameStarsDownloadsRepos Using ThisPackages Using ThisMost Recent CommitTotal ReleasesLatest ReleaseOpen IssuesLicenseLanguage
Prisma31,84644221 hours ago4,993September 24, 20222,920apache-2.0TypeScript
Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB and CockroachDB
Graphql Engine29,8371a day ago17June 22, 20222,129apache-2.0TypeScript
Blazing fast, instant realtime GraphQL APIs on your DB with fine grained access control, also trigger webhooks on database events.
Postgraphile11,95576996 days ago156May 25, 202267mitTypeScript
Execute one command (or mount one Node.js middleware) and get an instant high-performance GraphQL API for your PostgreSQL database!
Dbal9,08147,9463,933a day ago99April 05, 2022198mitPHP
Doctrine Database Abstraction Layer
Records6,956150282 months ago11February 21, 201964iscPython
SQL for Humans™
Sqlboiler5,81797a day ago95August 28, 202286bsd-3-clauseGo
Generate a Go ORM tailored to your database schema.
Goose3,95465994 days ago46August 29, 202257otherGo
A database migration tool. Supports SQL migrations and Go functions.
Dbmate3,43217 days ago27March 25, 202216mitGo
:rocket: A lightweight, framework-agnostic database migration tool.
Xo3,3542915 days ago2April 16, 202142mitGo
Command line tool to generate idiomatic Go code for SQL databases supporting PostgreSQL, MySQL, SQLite, Oracle, and Microsoft SQL Server
Scenic3,18211411a month ago18February 13, 202223mitRuby
Versioned database views for Rails
Alternatives To Django Tenant Schemas
Select To Compare

Alternative Project Comparisons


PyPi version PyPi downloads Python versions Travis CI PostgreSQL

This application enables django powered websites to have multiple tenants via PostgreSQL schemas. A vital feature for every Software-as-a-Service website.

Django provides currently no simple way to support multiple tenants using the same project instance, even when only the data is different. Because we don't want you running many copies of your project, you'll be able to have:

  • Multiple customers running on the same instance
  • Shared and Tenant-Specific data
  • Tenant View-Routing

What are schemas

A schema can be seen as a directory in an operating system, each directory (schema) with it's own set of files (tables and objects). This allows the same table name and objects to be used in different schemas without conflict. For an accurate description on schemas, see PostgreSQL's official documentation on schemas.

Why schemas

There are typically three solutions for solving the multitenancy problem.

  1. Isolated Approach: Separate Databases. Each tenant has it's own database.
  2. Semi Isolated Approach: Shared Database, Separate Schemas. One database for all tenants, but one schema per tenant.
  3. Shared Approach: Shared Database, Shared Schema. All tenants share the same database and schema. There is a main tenant-table, where all other tables have a foreign key pointing to.

This application implements the second approach, which in our opinion, represents the ideal compromise between simplicity and performance.

  • Simplicity: barely make any changes to your current code to support multitenancy. Plus, you only manage one database.
  • Performance: make use of shared connections, buffers and memory.

Each solution has it's up and down sides, for a more in-depth discussion, see Microsoft's excellent article on Multi-Tenant Data Architecture.

How it works

Tenants are identified via their host name (i.e This information is stored on a table on the public schema. Whenever a request is made, the host name is used to match a tenant in the database. If there's a match, the search path is updated to use this tenant's schema. So from now on all queries will take place at the tenant's schema. For example, suppose you have a tenant customer at Any request incoming at will automatically use customer's schema and make the tenant available at the request. If no tenant is found, a 404 error is raised. This also means you should have a tenant for your main domain, typically using the public schema. For more information please read the setup section.

What can this app do?

As many tenants as you want

Each tenant has its data on a specific schema. Use a single project instance to serve as many as you want.

Tenant-specific and shared apps

Tenant-specific apps do not share their data between tenants, but you can also have shared apps where the information is always available and shared between all.

Tenant View-Routing

You can have different views for and, even though Django only uses the string after the host name to identify which view to serve.


Everyone loves magic! You'll be able to have all this barely having to change your code!

Setup & Documentation

This is just a short setup guide, it is strongly recommended that you read the complete version at

Your DATABASE_ENGINE setting needs to be changed to

    'default': {
        'ENGINE': 'tenant_schemas.postgresql_backend',
        # ..

Add the middleware tenant_schemas.middleware.TenantMiddleware to the top of MIDDLEWARE_CLASSES, so that each request can be set to use the correct schema.


Add tenant_schemas.routers.TenantSyncRouter to your DATABASE_ROUTERS setting, so that the correct apps can be synced, depending on what's being synced (shared or tenant).


Add tenant_schemas to your INSTALLED_APPS.

Create your tenant model

from django.db import models
from tenant_schemas.models import TenantMixin

class Client(TenantMixin):
    name = models.CharField(max_length=100)
    paid_until =  models.DateField()
    on_trial = models.BooleanField()
    created_on = models.DateField(auto_now_add=True)

Define on which model is your tenant model. Assuming you created Client inside an app named customers, your TENANT_MODEL should look like this:

TENANT_MODEL = "customers.Client" # app.Model

Now run migrate_schemas to sync your apps to the public schema.

python migrate_schemas --shared

Create your tenants just like a normal django model. Calling save will automatically create and sync/migrate the schema.

from customers.models import Client

# create your public tenant
tenant = Client(domain_url='',
                name='My First Tenant',

Any request made to will now automatically set your PostgreSQL's search_path to tenant1 and public, making shared apps available too. This means that any call to the methods filter, get, save, delete or any other function involving a database connection will now be done at the tenant's schema, so you shouldn't need to change anything at your views.

You're all set, but we have left key details outside of this short tutorial, such as creating the public tenant and configuring shared and tenant specific apps. Complete instructions can be found at

Popular Schema Projects
Popular Postgresql Projects
Popular Data Formats Categories

Get A Weekly Email With Trending Projects For These Categories
No Spam. Unsubscribe easily at any time.