Sub topic 5
One Policy per Command
Prerequisites
This chapter assumes that you know basic SELECT, INSERT, UPDATE, and DELETE statements, PostgreSQL roles, and table privileges. You should also understand the difference between a table privilege, such as GRANT SELECT, and a row-level security (RLS) policy.
A table privilege answers, “May this role use this command on the table?” An RLS policy answers, “Which rows may this role use?”
Match Policies to Commands
A policy can apply to every command or to one command:
FOR SELECTcontrols rows returned by queries.FOR INSERTcontrols rows that a role tries to create.FOR UPDATEcontrols both the existing row and the replacement row.FOR DELETEcontrols rows that a role tries to remove.FOR ALLapplies to all four commands.
The command determines which policy expressions PostgreSQL uses. USING filters existing rows. WITH CHECK validates rows after an insert or update.
| Command | Existing row condition | New row condition |
|---|---|---|
| `SELECT` | `USING` | None |
| `INSERT` | None | `WITH CHECK` |
| `UPDATE` | `USING` | `WITH CHECK` |
| `DELETE` | `USING` | None |
For example, a user might read and modify only documents that they own:
CREATE TABLE documents (
id bigserial PRIMARY KEY,
owner_name text NOT NULL DEFAULT current_user,
title text NOT NULL,
body text
);
ALTER TABLE documents ENABLE ROW LEVEL SECURITY;
CREATE POLICY documents_select
ON documents
FOR SELECT
USING (owner_name = current_user);
CREATE POLICY documents_insert
ON documents
FOR INSERT
WITH CHECK (owner_name = current_user);
CREATE POLICY documents_update
ON documents
FOR UPDATE
USING (owner_name = current_user)
WITH CHECK (owner_name = current_user);
CREATE POLICY documents_delete
ON documents
FOR DELETE
USING (owner_name = current_user);The UPDATE policy has both expressions for an important reason. USING finds rows the role may update. WITH CHECK prevents the role from changing an allowed row into one owned by somebody else.
When `FOR ALL` Is Appropriate
A single FOR ALL policy is useful when the same rule should govern every operation:
CREATE POLICY documents_owner
ON documents
FOR ALL
USING (owner_name = current_user)
WITH CHECK (owner_name = current_user);For SELECT and DELETE, PostgreSQL uses the USING expression. For INSERT, it uses the WITH CHECK expression. For UPDATE, it uses both.
Separate policies are clearer when commands have different rules. For example, users might read every published document but update only their own drafts. Separate policies make that distinction visible and easier to audit.
Command and Role Scope
A policy can target specific roles with TO:
CREATE POLICY documents_readers
ON documents
FOR SELECT
TO analyst, editor
USING (owner_name = current_user);The role still needs the corresponding table privilege:
GRANT SELECT ON documents TO analyst, editor;A policy does not grant table access by itself. PostgreSQL requires both the table privilege and a policy that permits the row.
How PostgreSQL Chooses a Policy
PostgreSQL considers policies that match both the command and the role. A policy for FOR SELECT does not control inserts. A policy for FOR INSERT does not hide rows from queries.
This separation helps you express rules such as:
- Users may select rows where
tenant_idmatches their tenant. - Users may insert rows only for their current tenant.
- Users may delete only rows marked as temporary.
- Administrators may update rows but not delete them.
Exercise
Why should an ownership policy for UPDATE usually include both USING and WITH CHECK?
Show answer
USING limits which existing rows the role may update, while WITH CHECK prevents the updated row from being assigned to an unauthorized owner.