Sub topic 1
Why Row-Level Security?
Row-level security (RLS) lets PostgreSQL decide which rows a database role may read or change. A policy is a rule attached to a table. The rule can use the current role, a tenant identifier, or values in the row.
RLS is useful when many users share one table but must see different data. A multi-tenant application might store every customer’s invoices in invoices. The application can query the same table for every customer. RLS makes PostgreSQL return only rows belonging to the current tenant.
Prerequisites
This chapter assumes that you know basic SELECT, INSERT, UPDATE, DELETE, and WHERE clauses. You should also understand PostgreSQL tables, indexes, roles, and GRANT statements. You will need access to psql or another PostgreSQL client.
Table privileges and row access
PostgreSQL checks access in layers:
- Table and column privileges decide whether a role may use a table or column.
- RLS policies decide which rows that role may use.
- The SQL operation still must be valid.
For example, GRANT SELECT ON invoices TO analyst permits the role to select from the table. It does not necessarily expose every row once RLS is enabled. A policy can limit the result to invoices owned by the analyst’s tenant.
Column privileges and RLS solve different problems. A column privilege can hide the credit_card_number column from a role. RLS can hide particular invoice rows. Use both when a role needs access to some columns in only some rows.
| Control | Main question | Example |
|---|---|---|
| Table privilege | May this role use the table? | `GRANT SELECT ON invoices TO analyst` |
| Column privilege | May this role use this column? | `GRANT SELECT (id, total) ...` |
| RLS policy | Which rows may this role use? | Only rows for the current tenant |
The basic working model
When RLS is enabled, PostgreSQL adds policy conditions to statements internally. A SELECT behaves as if it has an additional filter. An UPDATE must both find an allowed existing row and produce an allowed new row.
The following example gives each role access only to rows whose owner_name matches its role name:
CREATE TABLE documents (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
owner_name name NOT NULL DEFAULT current_user,
title text NOT NULL,
body text
);
ALTER TABLE documents ENABLE ROW LEVEL SECURITY;
CREATE POLICY documents_owner
ON documents
FOR ALL
USING (owner_name = current_user)
WITH CHECK (owner_name = current_user);USING controls which existing rows are visible or targetable. WITH CHECK controls whether a new or changed row is acceptable. The policy uses current_user, which is PostgreSQL’s name for the active database role.
Without a matching policy, enabled RLS usually produces default deny behavior for ordinary roles. A role may have SELECT on the table and still receive no rows. It may also be unable to insert a row that fails every policy’s check condition.
Why keep this rule in the database?
Application code can forget a filter, use the wrong tenant identifier, or expose a new endpoint without applying the same restriction. RLS places the row rule at the database boundary, where direct SQL, background jobs, and multiple application services face the same control.
RLS does not automatically make a design safe. Roles that bypass RLS, policy expressions that reveal information through errors or timing, and incorrect session context require separate attention. Later chapters examine those cases.
Exercise
A role has SELECT privilege on a table, but RLS is enabled and no policy permits the role to see rows. What should you expect from SELECT?
Show answer
The query can run, but ordinary rows are hidden and the result is normally empty. The SELECT privilege alone does not bypass RLS.