Full answer
Test data:
CREATE TABLE invoices (id int, tenant_id int, amount int);
INSERT INTO invoices VALUES (1, 10, 500), (2, 10, 700), (3, 20, 900);
ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON invoices
USING (tenant_id = current_setting('app.tenant_id', true)::int)
WITH CHECK (tenant_id = current_setting('app.tenant_id', true)::int);
-- a non-superuser application role that owns the table (superusers always bypass RLS)
DO $$ BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'ibank_app') THEN CREATE ROLE ibank_app NOLOGIN; END IF;
EXECUTE format('GRANT USAGE ON SCHEMA %I TO ibank_app', current_schema());
END $$;
ALTER TABLE invoices OWNER TO ibank_app;SET ROLE ibank_app; SELECT count(*) AS visible_as_owner FROM invoices;Result:
visible_as_owner
------------------
3
(1 row)ALTER TABLE invoices FORCE ROW LEVEL SECURITY; SELECT count(*) AS visible_without_tenant FROM invoices;Output:
visible_without_tenant
------------------------
0
(1 row)BEGIN; SET LOCAL app.tenant_id = '10'; SELECT id, amount FROM invoices ORDER BY id;PostgreSQL returns:
id | amount
----+--------
1 | 500
2 | 700
(2 rows)INSERT INTO invoices VALUES (4, 20, 100);Result on PostgreSQL 17:
ERROR: new row violates row-level security policy for table "invoices"ROLLBACK; BEGIN; SET LOCAL app.tenant_id = '20'; UPDATE invoices SET amount = amount + 1 RETURNING id, tenant_id, amount;Result:
id | tenant_id | amount
----+-----------+--------
3 | 20 | 901
(1 row)COMMIT; RESET ROLE;Output:
RESETThe demo switches to ibank_app, an ordinary role that owns the table, because superusers bypass RLS in every case. Even with RLS enabled, the table owner still sees all three rows, a common surprise in development where the app connects as the owner. After FORCE ROW LEVEL SECURITY, a session without a tenant setting sees nothing, because current_setting(..., true) returns NULL and the policy is never true. With tenant 10 set locally, only invoices 1 and 2 are visible, and inserting a row for tenant 20 fails with 'new row violates row-level security policy'. The UPDATE as tenant 20 touches only invoice 3, though it has no WHERE clause. Use a separate non-owner role for the application, SET LOCAL inside each transaction so pooled connections do not leak tenants, and index tenant_id.
CREATE TABLE invoices (id int, tenant_id int, amount int);
INSERT INTO invoices VALUES (1, 10, 500), (2, 10, 700), (3, 20, 900);
ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON invoices
USING (tenant_id = current_setting('app.tenant_id', true)::int)
WITH CHECK (tenant_id = current_setting('app.tenant_id', true)::int);
-- a non-superuser application role that owns the table (superusers always bypass RLS)
DO $$ BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'ibank_app') THEN CREATE ROLE ibank_app NOLOGIN; END IF;
EXECUTE format('GRANT USAGE ON SCHEMA %I TO ibank_app', current_schema());
END $$;
ALTER TABLE invoices OWNER TO ibank_app;
SET ROLE ibank_app; SELECT count(*) AS visible_as_owner FROM invoices;
ALTER TABLE invoices FORCE ROW LEVEL SECURITY; SELECT count(*) AS visible_without_tenant FROM invoices;
BEGIN; SET LOCAL app.tenant_id = '10'; SELECT id, amount FROM invoices ORDER BY id;
INSERT INTO invoices VALUES (4, 20, 100);
ROLLBACK; BEGIN; SET LOCAL app.tenant_id = '20'; UPDATE invoices SET amount = amount + 1 RETURNING id, tenant_id, amount;
COMMIT; RESET ROLE;Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.
Key points
- Policies filter rows for every query
- WITH CHECK guards writes
- Owners bypass RLS unless FORCE is set
- SET LOCAL per transaction with pooling
- Missing setting means no rows
Common mistakes
- Testing RLS as the table owner
- Using session-level SET with transaction pooling
Follow-ups
- How does RLS affect query plans and performance?
- How would you give support staff cross-tenant read access?
Tips
- Show the owner bypass