Choosing the Right Tenant Isolation Model
Every B2B SaaS platform must answer a foundational question early: how do we keep Organization A's data strictly invisible to Organization B?
PostgreSQL offers three primary patterns: shared tables with a tenant_id column enforced by Row-Level Security (RLS), separate schemas per tenant, or dedicated databases for high-compliance enterprise accounts.
Why Row-Level Security Fits Most Modern SaaS
With RLS, database migrations run once across a single schema, connection pooling stays efficient, and the database itself filters rows by tenant even if an application query forgets to, provided the application connects with a role that policies apply to.
ALTER TABLE projects ENABLE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON projects
USING (tenant_id = current_setting('app.current_tenant_id')::uuid);Three Mistakes That Undo Row-Level Security
Policies protect you only when they actually apply to the connection running the query.
- Table owners skip policies unless you force them, and superusers or roles with BYPASSRLS always skip them. Connect with a restricted application role.
- With a connection pool, set the tenant inside each transaction so one request cannot inherit another's value.
- Add tests that sign in as one tenant and try to read and write another tenant's rows.
ALTER TABLE projects FORCE ROW LEVEL SECURITY;
BEGIN;
SELECT set_config('app.current_tenant_id', $1, true); -- true: this transaction only
SELECT * FROM projects;
COMMIT;When to Isolate Further
Separate schemas can help when tenants need slightly different structures, at the cost of running every migration many times. Separate databases suit customers with strict residency or recovery requirements, and cost the most to operate. Many products start shared and move a few accounts out when a contract requires it.



