Sub topic 3
Writing Your First Policy
This chapter assumes you know basic SELECT, INSERT, UPDATE, and DELETE statements, can create PostgreSQL tables and indexes, and understand roles and GRANT. Row-level security (RLS) controls which rows an already-authorized role may access. It does not replace table privileges.
Start with a default-deny table
Create a table containing an owner for each document. Enabling RLS changes the table's behavior: roles that do not bypass RLS can access no rows unless a policy allows them to do so.
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;current_user is PostgreSQL's name for the role currently executing the statement. The default value makes the inserting role the document owner unless the statement supplies another value.
At this point, a role may still need ordinary table privileges:
GRANT SELECT, INSERT, UPDATE, DELETE
ON documents
TO alice, bob;
GRANT USAGE
ON SEQUENCE documents_id_seq
TO alice, bob;The first GRANT permits operations on the table. RLS policies then limit which rows those operations can use.
Create an owner policy
A policy attaches a Boolean expression to a table operation. PostgreSQL evaluates the expression for each relevant row.
CREATE POLICY documents_owner ON documents
FOR ALL
USING (owner_name = current_user)
WITH CHECK (owner_name = current_user);This policy says:
FOR ALLapplies the policy toSELECT,INSERT,UPDATE, andDELETE.USINGcontrols which existing rows the role can see or target.WITH CHECKcontrols whether a new or changed row is allowed.- The role can use a row only when its
owner_namematches the current role.
FOR ALL is convenient when the same rule applies to every command. You can also create separate policies for different commands when, for example, users may read shared rows but update only their own rows.
USING versus WITH CHECK
For a SELECT, USING determines which rows appear. For DELETE, it determines which rows can be targeted. For INSERT, there is no existing row to filter, so WITH CHECK validates the proposed row. For UPDATE, USING filters the old row and WITH CHECK validates the new version.
This distinction prevents a user from changing a row they own into a row owned by someone else. Without WITH CHECK, an update policy could accidentally allow ownership changes that defeat the intended isolation rule.
Test the policy
Switch roles in a test session to observe the result:
SET ROLE alice;
INSERT INTO documents (title, body)
VALUES ('Alice notes', 'Private text');
SET ROLE bob;
SELECT title FROM documents;
-- Returns no rows
INSERT INTO documents (title, owner_name)
VALUES ('Forged ownership', 'alice');
-- ERROR: new row violates row-level security policy
RESET ROLE;The failed insert demonstrates WITH CHECK: Bob cannot create a row claiming to belong to Alice.
Exercise
Which expression prevents Bob from inserting a document with owner_name = 'alice'?
Show answer
The WITH CHECK expression, ownername = currentuser, rejects the proposed row because alice does not equal Bob's current role.