Sub topic 6
Targeting Policies at Roles
This chapter assumes that you know basic SQL privileges, PostgreSQL roles, and GRANT. A policy controls which rows a role may use. It does not grant table privileges. The role still needs privileges such as SELECT or INSERT.
The `TO` Clause
Add TO to CREATE POLICY to limit a policy to one or more database roles:
CREATE POLICY policy_name
ON table_name
FOR command
TO role_name
USING (row_condition)
WITH CHECK (new_row_condition);You can list several roles, use PUBLIC for every role, or omit TO. Omitting TO has the same effect as TO PUBLIC.
For example, this policy lets alice and bob read rows that they own:
CREATE POLICY documents_select
ON documents
FOR SELECT
TO alice, bob
USING (owner_name = current_user);The TO clause answers “who may use this policy?” The USING expression answers “which existing rows may that role see?”
A role can receive access through membership in another role. For example, if support_staff has a policy and alice is a member of support_staff, the policy can apply to Alice according to the role's membership and inheritance settings.
| Policy target | Meaning |
|---|---|
| `TO alice` | Applies to Alice |
| `TO alice, bob` | Applies to either role |
| `TO support_staff` | Applies to members who use that role |
| `TO PUBLIC` | Applies to every role |
Combining Role-Specific Policies
PostgreSQL can have several policies for the same table and command. Permissive policies, which are the default, combine with OR. A row is allowed when at least one applicable policy allows it.
Suppose a company has regular users and auditors:
CREATE POLICY documents_owner
ON documents
FOR SELECT
TO alice, bob
USING (owner_name = current_user);
CREATE POLICY documents_audit
ON documents
FOR SELECT
TO auditors
USING (true);Alice sees only her own rows. An auditor sees every row allowed by the policy. If Alice also belongs to auditors, both policies apply, so she can see every row.
This is a common mistake: adding a broad policy for a role can expand access for users who belong to that role.
A Complete Role-Based Example
The following setup gives Alice and Bob access to the table, then uses a policy to restrict each user to owned rows:
CREATE TABLE documents (
id bigserial PRIMARY KEY,
owner_name text NOT NULL DEFAULT current_user,
title text NOT NULL
);
ALTER TABLE documents ENABLE ROW LEVEL SECURITY;
CREATE ROLE alice;
CREATE ROLE bob;
GRANT SELECT, INSERT, UPDATE, DELETE
ON documents TO alice, bob;
GRANT USAGE, SELECT
ON SEQUENCE documents_id_seq TO alice, bob;
CREATE POLICY documents_user_access
ON documents
FOR ALL
TO alice, bob
USING (owner_name = current_user)
WITH CHECK (owner_name = current_user);The FOR ALL policy applies to SELECT, INSERT, UPDATE, and DELETE. The USING condition filters rows that already exist. The WITH CHECK condition validates rows created or changed by the command.
The table owner or a role with BYPASSRLS can bypass these policies unless you use FORCE ROW LEVEL SECURITY. That decision belongs to the table's security design, not to the TO clause.
Reasoning About Effective Access
When debugging, inspect both grants and policies:
SELECT policyname, roles, cmd, permissive, qual, with_check
FROM pg_policies
WHERE tablename = 'documents';Exercise
Alice belongs to both editors and auditors. An editors policy allows rows where owner_name = current_user, while an auditors policy uses USING (true). Which rows can Alice select?
Show answer
Both policies apply. Because the default policies are permissive and combine with OR, Alice can select every row allowed by the auditor policy.