GuideAdvanced2 min read
PostgreSQL RLS for Tenant Isolation: The Failure Modes
Protect tenant data by understanding RLS bypass roles, session context, pool boundaries and cross-tenant authorization tests.
- Author
- Pradeep Gajendra
- Published
- Updated
Scope of this article
- Versions
- PostgreSQL 16
- Environment
- SQL assumes a separately provisioned app_runtime role and tenant-aware authentication.
Adding tenant_id to every table is not sufficient isolation. The vulnerability usually appears at a boundary: a background job without tenant context, a privileged migration connection, an admin query that forgets a filter, or an RLS session variable leaked through pooled connections.
Specify the trust boundary#
A safe multi-tenant design verifies the authenticated tenant's membership, scopes every query, tests roles actually used by the application and prevents the application role from owning protected tables. PostgreSQL's RLS applies only to roles subject to the policy; owners and roles with BYPASSRLS may bypass it [1].
Policy and role separation#
-- Migration role owns the table; application role is non-owner.
alter table invoices enable row level security;
alter table invoices force row level security;
create policy invoice_tenant on invoices
for all to app_runtime
using (tenant_id = nullif(current_setting('app.tenant_id', true), '')::uuid)
with check (tenant_id = nullif(current_setting('app.tenant_id', true), '')::uuid);FORCE ROW LEVEL SECURITY makes table owners subject to RLS, but not superusers or BYPASSRLS roles. Keep the application role minimally privileged, and grant only operations it actually needs.
Bind tenant context to a transaction#
begin;
select set_config('app.tenant_id', $1, true);
select id, amount_cents from invoices where status = 'open';
commit;The third set_config parameter makes the value transaction-local. It must be set on the same checked-out connection as the queries. Never use a session-global setting with transaction-pooling or assume a request-local JavaScript variable magically scopes SQL.
Test cross-tenant denial explicitly#
Actor | Attempt | Expected |
|---|---|---|
Tenant A user | Read tenant B invoice by UUID | No rows / authorized 404 |
Tenant A user | Insert invoice with tenant B ID | Policy check rejection |
Runtime role | Query without tenant context | Deny by default |
Background job | Run a query without setting tenant | Fail closed |
Migration/owner role | Query with missing isolation | Alerted privileged-path review |
Do not expose raw SQL errors to clients. Integrate tests with the actual production database roles, pooled connections and job executors. Seed two tenants with deliberately similar IDs so a forgotten predicate cannot hide behind sparse data.
Sources
- [1]PostgreSQL Documentation: 5.8 Row Security Policies
PostgreSQL Global Development Group · publication date unknown · PostgreSQL 16 · retrieved 2026-11-02
Spotted an error? Use “Suggest a correction” in the discussion, or read our corrections policy.
Discussion
Sign in to join the discussion. Reading is always open.
Loading discussion…