The key points
- Shared tables with a tenant identifier on every row is the cheapest model to run and the right default for a product with many small customers.
- Row-level security makes the database refuse cross-tenant reads, but only if the app connects with a role that cannot bypass it.
- Schema per tenant and database per tenant buy stronger separation at the cost of more migrations, more connections and more operations work.
- A tenant-to-deployment mapping lets one large or regulated customer move later without rewriting the application.
What are the three ways to separate customer data?
A tenant is one customer organization using your product, usually with many users inside it. Every SaaS decides how tenants share a database, and there are three common answers. With shared tables, every customer's rows live in the same tables and each row carries a tenant identifier. With a schema per tenant, one database holds a separate set of tables for each customer. With a database per tenant, each customer gets a database of its own.
Microsoft's Azure Architecture Center guidance on tenancy models treats this as a spectrum from fully shared to fully isolated, not a switch, and says the choice is a commercial decision as well as a technical one. It also notes that when customers are businesses, a tenant usually maps to one customer organization and holds many users.
AWS's SaaS Architecture Fundamentals whitepaper describes the same range: in some designs entire stacks of resources are dedicated to a tenant, and in others pooled resources need finer-grained policies to control access.
Which model should an early-stage SaaS start with?
Start with shared tables unless a signed customer already requires more. Azure's guidance says that if you expect many customers you should share infrastructure, because otherwise you maintain a large and growing fleet of instances, and that a few customers with high isolation needs may justify dedicated resources. Most products at the MVP stage sit in the first group.
The price of sharing is real. The same guidance warns that you must separate each tenant's data and not leak it between tenants, and that a large tenant running a heavy query can affect the others. Those two risks, leakage and noisy neighbours, are what the rest of the design has to answer.
Put the tenant identifier on every tenant-owned table from the first migration. Adding it later means touching every table, every query and every index at once, the expensive rewrite that retrofitting tenancy usually becomes. Softivum's SaaS development process settles tenancy in a two-week architecture phase before the build starts, because it shapes everything after it.
What does PostgreSQL row-level security do, and what does it not do?
Row-level security (RLS) lets the database itself restrict which rows a query can see. The PostgreSQL documentation says that once RLS is enabled on a table, all normal access must be allowed by a policy, and if no policy exists a default-deny policy applies, so no rows are visible or modifiable. A policy that compares each row's tenant identifier with the current request's tenant turns a forgotten WHERE clause into an empty result instead of a data leak.
RLS has gaps you have to close. The same page says table owners normally bypass row security unless the table is set with FORCE ROW LEVEL SECURITY, and that superusers and roles with the BYPASSRLS attribute always bypass it. OWASP's Multi-Tenant Security Cheat Sheet therefore asks for a request role that is neither a superuser nor BYPASSRLS, tenant context set with transaction-local settings, and a design that fails closed when the tenant is missing. It also asks for policy coverage on every tenant-owned table.
RLS is a second lock, not the only one. The AWS whitepaper makes the point that an authenticated, authorized user can still reach another tenant's resources if nothing enforces isolation separately. OWASP adds that a tenant identifier sent by the browser is only a selector: the server must check that the signed-in user may act in that tenant, and should answer 404 rather than 403 for another tenant's objects so the response does not confirm they exist.
- Connect the application with an ordinary role, never the table owner or a superuser.
- Set the tenant for each transaction, and make a missing tenant return nothing.
- Check membership on the server before trusting any tenant identifier from the client.
When does a schema per tenant make sense?
A schema per tenant suits a moderate number of customers who each need their own tables, for example per-customer custom fields or a simple per-customer export. In PostgreSQL the same table name can exist in many schemas, and by default users cannot reach objects in schemas they do not own, according to the PostgreSQL schemas documentation.
Schemas are a namespace, not a hard wall. The same page says a user can access objects in any schema they have privileges on, and warns that adding a schema to the search path trusts everyone who can create objects in it. OWASP likewise says separate schemas need disciplined grants, role separation and search path handling.
The operating cost shows up at release time. Every schema change has to run once per customer, and a failure halfway leaves tenants on different versions of the database. That cost grows with each customer you sign, which is why this model rarely beats shared tables for a product with many small accounts.
When do customers force a database per tenant?
A database per tenant is the right answer when a customer's contract, regulator or security team demands it, or when one customer's workload is far heavier than the rest. Azure's guidance lists the benefits: tenant data is isolated, which reduces the risk of leakage, tenants are unlikely to affect each other's performance, and updates can roll out progressively.
The same guidance lists the costs. Cost efficiency is low because nothing is shared, and routine maintenance across many deployments takes time, so it recommends automating operations. It also notes that cross-tenant reporting needs a plan when data sits in many places.
You rarely have to pick one model for everyone. Azure describes vertical partitioning, where most tenants share infrastructure and a few have dedicated deployments, and says you can charge more for the dedicated option. It also describes a horizontal split, a shared application tier with a database per tenant, which targets the noisy neighbour problem. To keep that door open, store a mapping from each tenant to the deployment that holds its data and route every request through it.
How do you stop one customer from slowing the others?
Add the tenant as a dimension of every limit. OWASP's cheat sheet recommends tenant identity as one rate-limit key alongside global, endpoint, user and IP limits, and warns that limits at the HTTP edge do not constrain every shared resource. Queue depth, concurrency, database connections and compute need their own bounds, and over-limit requests should receive HTTP 429 with a Retry-After header.
Where a service commitment justifies the cost, give heavy customers their own worker pool or their own database. Record usage per tenant from the first release, so you know which customer is the heavy one before the support tickets say so.
How do you prove tenants cannot see each other's data?
Write the proof as automated tests and run them on every release. OWASP's cheat sheet lists what to test: that cross-tenant operations are denied and same-tenant ones succeed, that the list of tenant-owned tables comes from the schema so a new table without a policy fails the build, that the deployed request role has no superuser or bypass privilege, and that requests for two tenants over reused connections never leak context.
It also asks for a restore test with two tenants: recover one and confirm the other's data is absent. Enterprise buyers ask about exactly these points in a security review, so the same evidence serves a sale. The B2B portal access control answer applies the same boundary to dealer and supplier portals, and the secure software development checklist covers the wider habits.
Three ways to hold customer data in a multi-tenant SaaS
| Question | Shared tables | Schema per tenant | Database per tenant |
|---|---|---|---|
| Where customer data sits | Same tables, tenant identifier on every row | Separate tables per customer in one database | A whole database per customer |
| Isolation enforced by | Application checks plus row-level security | Schema grants, roles and search path discipline | Separate credentials and connections |
| Cost to run | Lowest, one set of infrastructure | Moderate, one database with many schemas | Highest, a fleet of databases to run and patch |
| Releasing a schema change | Once | Once per customer | Once per customer, across databases |
| One customer's heavy load | Can slow others, needs per-tenant limits | Same engine shared, so still a risk | Contained to that customer's database |
| Best when | Many small or medium customers | A modest number of customers needing custom tables | Regulated or very large customers, or a contract that demands it |
Tenancy decisions to settle before the first migration
- Define who a tenant is: a customer organization, a team inside it, or an individual.
- Put a tenant identifier on every tenant-owned table and index it.
- Enable row-level security with a policy on each of those tables and a request role that cannot bypass it.
- Verify tenant membership on the server for every request, never from a client-sent identifier alone.
- Keep a tenant-to-deployment mapping so a customer can be moved later.
- Add per-tenant rate limits and usage metering.
- Automate cross-tenant tests and a two-tenant restore test in the release pipeline.
Sources
Rules and guidance this answer relies on. Policies change, so check the current version before acting on it.
