Sub topic 2
One policy per command
This chapter assumes you know basic SQL, can create tables and roles in PostgreSQL, and have already run ALTER TABLE ... ENABLE ROW LEVEL SECURITY on a table. Without any policies, an RLS-enabled table returns no rows to non-bypassing roles.
One policy per command
A policy applies to one command (SELECT, INSERT, UPDATE, or DELETE) or to all four with FOR ALL. Separate policies let you write different rules for reading and writing.
CREATE POLICY documents_select_own ON documents
FOR SELECT
USING (owner_name = current_user);
CREATE POLICY documents_insert_own ON documents
FOR INSERT
WITH CHECK (owner_name = current_user);USING filters existing rows that a command can read or modify. WITH CHECK validates new rows that a command writes.
Targeting policies at roles
The TO clause limits a policy to named roles. If you omit it, the policy applies to PUBLIC, which means every role.
CREATE POLICY documents_select_team ON documents
FOR SELECT
TO alice, bob
USING (owner_name = current_user);Combining permissive and restrictive policies
Policies are permissive by default. For one command, PostgreSQL combines permissive policies with OR, so a row passes if any one of them allows it. A restrictive policy (AS RESTRICTIVE) combines with AND, so a row must also pass it.
CREATE POLICY hide_archived ON invoices
AS RESTRICTIVE
FOR SELECT
USING (NOT archived);Who bypasses RLS, and FORCE ROW LEVEL SECURITY
Superusers and roles with the BYPASSRLS attribute always skip policies. Table owners also skip them by default. To apply policies to the owner, run:
ALTER TABLE invoices FORCE ROW LEVEL SECURITY;An application that connects as the table owner sees every row until you run this command.
Passing the current user or tenant into a policy
A policy can compare a column with current_user or with a session setting. For multi-tenant apps, store the tenant ID with set_config and pass is_local = true, which limits the value to the current transaction.
CREATE POLICY tenant_isolation ON invoices
USING (tenant_id = NULLIF(current_setting('app.tenant_id', true), '')::uuid)
WITH CHECK (tenant_id = NULLIF(current_setting('app.tenant_id', true), '')::uuid);
BEGIN;
SELECT set_config('app.tenant_id', '3f2b8c1e-5a4d-4e0b-9f6a-1c2d3e4f5a6b', true);
SELECT id, amount_cents FROM invoices;
COMMIT;The true argument to current_setting returns NULL when the setting is unset, so a query without a tenant sees no rows.
Exercise
A permissive policy lets alice SELECT rows where ownername = currentuser. A restrictive policy FOR SELECT USING (NOT archived) also exists. Can alice see an archived row she owns?
Show answer
No. The row passes the permissive policy (OR), but it fails the restrictive policy (AND) because archived is true.
Exercise
After ENABLE ROW LEVEL SECURITY, the table owner still sees all rows. What change makes the policies apply to the owner?
Show answer
Run ALTER TABLE ... FORCE ROW LEVEL SECURITY. Superusers and BYPASSRLS roles still skip policies.