PostgreSQL Row-Level Security for Safer Multi-Tenant Data Access
Enforce tenant filters in the database, constrain application roles, and avoid leaking pooled session state.
PostgreSQL row-level security (RLS) lets a table define which rows a role may read or modify. Policies can add a database-enforced tenant predicate to ordinary queries, providing a defense layer against forgotten application filters.
THE PRODUCTION PROBLEM STATEMENT
In a multi-tenant system, one missing WHERE tenant_id clause can expose another customer’s rows. RLS makes access conditions part of the table policy, but it is only effective when roles, connection state, and policy checks are configured correctly.
Enable row security and constrain reads and writes
Enable row-level security on each protected table and create policies for the operations the application role performs. USING controls which existing rows a command can see or target; WITH CHECK controls the rows a command may create or leave behind.
Write policies for each operation deliberately. A SELECT policy does not automatically provide the right INSERT, UPDATE, or DELETE behavior.
ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
CREATE POLICY invoice_tenant_access ON invoices
USING (tenant_id = current_setting('app.tenant_id')::uuid)
WITH CHECK (tenant_id = current_setting('app.tenant_id')::uuid);
BEGIN;
SET LOCAL app.tenant_id = '00000000-0000-0000-0000-000000000001';
SELECT id, total FROM invoices;
COMMIT;Use a restricted role and a transaction-scoped tenant
Table owners normally bypass row security, as do roles with BYPASSRLS and superusers. Run application queries through a least-privilege role and consider FORCE ROW LEVEL SECURITY when the table owner should also be subject to policies.
In a connection pool, avoid leaving a tenant value as session state that can leak to the next request. Set it transaction-locally only after authenticating the request, and keep the tenant identifier out of user-controlled SQL.
Test the policy as the real application role
Test access as the same non-owner role used by the service. Cover cross-tenant reads, inserts with another tenant ID, updates that move a row between tenants, and requests without tenant context.
RLS is a database safeguard, not a replacement for authorization design or query review. Migrations, reporting roles, background jobs, and administrative access need explicit policies too.
Frequently Asked Questions & Technical Inquiries
Direct, practical answers to common architectural and debugging questions.
By default, table owners typically bypass row security. FORCE ROW LEVEL SECURITY can subject the owner to policies, while superusers and BYPASSRLS roles still bypass them.
Faisal Askari
Verified AuthorSoftware Engineer & Full-Stack Developer
Faisal Askari is a software engineer with deep expertise in web systems architecture, the JavaScript runtime ecosystem, and client-side performance engineering. Over years of hands-on production engineering, he has designed and shipped enterprise dashboards, high-converting digital interfaces, and specialized full-stack applications.
Resilient Full-Stack Architecture: Idempotent APIs, Optimistic UI, and Cache Invalidation
Engineering robust distributed state between client and server: idempotency keys, safe optimistic rollbacks, exponential backoff with jitter, and cache coherence.
Server-Sent Events (SSE) vs WebSockets over HTTP/2: Choosing the Right Real-Time Streaming Architecture
Why HTTP/2 multiplexing made Server-Sent Events the modern standard for LLM token streams and live telemetry over complex WebSockets.