Prompt
How do I know if one PostgreSQL database can support multiple tenants?
Latest observation
A single PostgreSQL database can support multiple tenants, but whether it’s a good fit depends on your isolation, scale, security, and operational requirements.
Quick rule of thumb
If tenants can safely share the same schema/data model and you’re okay enforcing separation in the app or database, then one database can support many tenants. If tenants need strong isolation, separate compliance boundaries, or custom per-tenant tuning, you may want separate databases.
Common multi-tenant patterns in PostgreSQL
1) Shared tables, tenant_id column
All tenants share the same tables, with a tenant_id column on every tenant-owned row.
Best when:
- Many small/medium tenants
- Similar schema for all tenants
- You want simpler operations and lower cost
Needs:
- Every query must filter by
tenant_id - Strong access controls to prevent cross-tenant leaks
- Good indexing, usually including
(tenant_id, ...)
Example:
SELECT * FROM orders WHERE tenant_id = $1;
2) Schema per tenant
Each tenant gets their own schema in the same database.
Best when:
- Tenants need moderate isolation
- You may customize schema per tenant
- Tenant count is not huge
Tradeoff:
- More operational overhead
- Migrations are more complex
3) Database per tenant
Each tenant gets a separate PostgreSQL database.
Best when:
- Strong isolation is required
- Compliance/security matters a lot
- Tenants are large or customized
Tradeoff:
- Highest operational cost
- Harder to manage at scale
How to tell if one database is enough
Ask these questions:
1) How strong must tenant isolation be?
- Weak/moderate isolation acceptable → shared database is viable
- Strong legal/compliance isolation required → consider separate DBs
2) How many tenants and how big are they?
- Hundreds/thousands of small tenants → shared DB often works well
- A few huge tenants → may need partitioning, dedicated schemas, or separate DBs
3) Do tenants need custom schema changes?
- No → shared tables are fine
- Yes → schema-per-tenant or database-per-tenant is easier
4) Can your app reliably enforce tenant scoping?
If yes, shared DB is possible.
If no, it’s risky.
5) What’s your backup/restore requirement?
Can you restore one tenant without affecting others?
If not, shared DB may be limiting.
6) What’s your growth and performance profile?
Look for:
- Large tables growing very fast
- Queries that don’t filter by tenant
- Hot tenants dominating CPU/IO
- Vacuum and bloat issues
If these are manageable with indexes/partitioning, shared DB can still work.
Technical signs that a shared PostgreSQL DB will work
A single database is usually a good fit if:
- All tenants use the same schema
- Tenant data volume is moderate or can be partitioned
- You can enforce
tenant_ideverywhere - You have proper indexes on tenant-scoped columns
- You can prevent cross-tenant access with:
- application logic
- PostgreSQL Row Level Security (RLS)
- separate roles/permissions
- You don’t need tenant-specific extensions or configuration
Warning signs that you may need separate databases
Consider moving away from one DB if:
- Tenants must be fully isolated for compliance
- You need per-tenant backups, restores, or exports frequently
- One tenant can overwhelm shared resources
- You need different retention, indexing, or maintenance schedules per tenant
- Tenant count is low but each tenant is very large
- You cannot trust every query path to include tenant filtering
Strong recommendation: use Row Level Security if sharing tables
If you use a shared-table design, PostgreSQL Row Level Security (RLS) is one of the best ways to protect tenant data.
It helps ensure that even if the app makes a mistake, PostgreSQL blocks access to rows from other tenants.
Practical decision guide
- Start with one database if your tenants are similar and small/medium.
- Use shared tables + tenant_id + indexes + RLS for the simplest scalable approach.
- Move to schema-per-tenant or database-per-tenant only if you hit isolation, compliance, or operational limits.
A simple test
A single PostgreSQL database is probably enough if you can answer “yes” to most of these:
- Can all tenants share the same schema?
- Can every tenant query be correctly scoped?
- Is strong physical isolation not required?
- Can backups/restores be done at database level instead of per tenant?
- Can you handle growth with indexing/partitioning?
If you want, I can help you decide between shared tables vs schema-per-tenant vs database-per-tenant based on your tenant count, compliance needs, and workload.
Brands