PostgreSQL RLS: tenant isolation in commerce SaaS

RLS (Row Level Security) is a PostgreSQL mechanism that filters rows by tenant_id, even if application code builds a flawed query.
Shared DB vs separate schemas
RLS fits many small tenants sharing infrastructure cost. Separate schemas or clusters appear for strict compliance.
The call is risk-driven, not “multi-tenant” fashion.
Policies and app roles
We set `SET app.tenant_id` per request in NestJS and verify bare queries return zero rows.
SQL migrations must create policies idempotently, or staging and prod diverge within weeks.
Testing and recovery
“Tenant A never sees tenant B” scenarios live in integration suites. Backups follow per-tenant retention.
We rehearse restores in isolation before the first production incident.
Frequently asked questions
- Does RLS replace app tests?
- No. it is the last line. We still enforce auth in code and audit endpoints.
- Performance impact?
- tenant_id indexes and conscious queries are mandatory; we profile plans under peak load.
- What about GDPR?
- RLS helps technically; erasure flows and DPIA still need legal process and logging.
Content updated: March 14, 2026