Browse topics

Row-level security policies

Version
19
Since
9.5
Review
Draft

Summary#

Enable row-level security to apply policies on top of table grants. In an ALL policy, USING controls which existing rows the role can see or target. WITH CHECK tests the proposed rows of INSERT and UPDATE. A check that returns false or null rejects the write.

Syntax#

sql
ALTER TABLE table_name ENABLE ROW LEVEL SECURITY;
CREATE POLICY policy_name ON table_name
    FOR ALL TO app_role
    USING (existing_row_predicate)
    WITH CHECK (new_row_predicate);

-- To apply policies to the table owner as well:
ALTER TABLE table_name FORCE ROW LEVEL SECURITY;

Example#

Run psql -X -q -A -P null=NULL -v ON_ERROR_STOP=1 -f example.sql as a superuser on a disposable PostgreSQL 19 cluster. The script creates two roles and switches between them in one transaction. The application role inserts its own team's ticket and catches SQLSTATE 42501 when it tries to insert for another team. It then lists its visible tickets. The non-superuser table owner sees every ticket that was stored. ROLLBACK removes the roles and schema.

sql
BEGIN;

CREATE ROLE dlr_cases_owner NOLOGIN NOSUPERUSER NOBYPASSRLS;
CREATE ROLE dlr_cases_app NOLOGIN NOSUPERUSER NOBYPASSRLS;
CREATE SCHEMA dlr_cases AUTHORIZATION dlr_cases_owner;
SET LOCAL ROLE dlr_cases_owner;

CREATE TABLE dlr_cases.tickets (
    ticket_id integer PRIMARY KEY,
    team_role name NOT NULL
);
INSERT INTO dlr_cases.tickets VALUES (1, 'dlr_cases_app'), (2, 'other_team');

ALTER TABLE dlr_cases.tickets ENABLE ROW LEVEL SECURITY;
CREATE POLICY team_tickets ON dlr_cases.tickets
    FOR ALL TO dlr_cases_app
    USING (team_role = current_user)
    WITH CHECK (team_role = current_user);

GRANT USAGE ON SCHEMA dlr_cases TO dlr_cases_app;
GRANT SELECT, INSERT ON dlr_cases.tickets TO dlr_cases_app;

SET LOCAL ROLE dlr_cases_app;
INSERT INTO dlr_cases.tickets VALUES (3, 'dlr_cases_app');

DO $$
BEGIN
    INSERT INTO dlr_cases.tickets VALUES (4, 'other_team');
    RAISE EXCEPTION 'expected the policy to reject ticket 4';
EXCEPTION
    WHEN insufficient_privilege THEN NULL;
END;
$$;

SELECT 'cross-team insert rejected (42501)' AS write_check;
SELECT current_user, ticket_id FROM dlr_cases.tickets ORDER BY ticket_id;

SET LOCAL ROLE dlr_cases_owner;
SELECT current_user, ticket_id FROM dlr_cases.tickets ORDER BY ticket_id;

ROLLBACK;

Expected output#

Text
write_check
cross-team insert rejected (42501)
(1 row)
current_user|ticket_id
dlr_cases_app|1
dlr_cases_app|3
(2 rows)
current_user|ticket_id
dlr_cases_owner|1
dlr_cases_owner|2
dlr_cases_owner|3
(3 rows)

Common mistakes#

ERROR: new row violates row-level security policy for table means that an inserted or updated row failed the policy's check, or that no policy permits that command for the role.

Give the application role the schema and table grants it needs. With row-level security enabled, access defaults to denial when no policy applies. Test with the application's effective role. Table owners bypass policies by default and can opt in with FORCE ROW LEVEL SECURITY. Superusers and roles with BYPASSRLS still bypass policies. For an ALL or UPDATE policy, leaving out WITH CHECK reuses the USING expression for new rows.

Version applicability#

PostgreSQL 9.5 introduced row-level security, including CREATE POLICY, the USING and WITH CHECK clauses, and the ENABLE and FORCE table options. The example uses the policy syntax available in that release.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0