Multi-tenant database architectures: Shared schemas vs. isolated databases
When building a B2B SaaS application, the architectural question that defines your system for the next five years is: "How do we isolate customer data?" Get it wrong, and a bug in a WHERE clause might show one enterprise customer another customer's confidential invoices.
Pattern 1: Shared database, shared schema with Row-Level Security (RLS)
Every table has a `tenant_id` column. In older systems, developers had to remember to add `WHERE tenant_id = current_tenant` to every single SQL query. Forget it once, and you have a data leak.
Modern PostgreSQL solves this with Row-Level Security (RLS). You set the tenant context on the database connection `SET LOCAL app.current_tenant_id = 'xyz'`, and Postgres automatically enforces tenant isolation at the database kernel level. Even if application code executes `SELECT * FROM users`, Postgres only returns rows belonging to that tenant.
Pros: Minimal infrastructure cost, easy migrations. Cons: Noisy neighbor problems if one tenant runs massive queries.
Pattern 2: Separate schemas per tenant
Each tenant gets their own schema inside a shared Postgres instance (`tenant_a.orders`, `tenant_b.orders`).
Pros: Logical separation, easier per-tenant backups. Cons: Migration nightmares. Running an `ALTER TABLE` across 2,000 schemas during a deployment can take hours and lock connection pools.
Pattern 3: Database per tenant
Every enterprise customer gets a dedicated RDS instance or isolated serverless database.
This is mandatory for healthcare (HIPAA) or enterprise banking clients who refuse to share physical compute or storage with other organizations. The operational complexity is high, but with infrastructure-as-code (Terraform) it can be automated seamlessly.
Our rule of thumb
Start with PostgreSQL Row-Level Security for standard tiers. Offer dedicated isolated database deployments as an enterprise tier upsell.
Have a similar challenge?
We build production-grade software for companies that need it done right.
Let's Talk
