skip to content

In PostgreSQL, what happens to an ordinary role's SELECT, UPDATE and INSERT on a table after ENABLE ROW LEVEL SECURITY but before any CREATE POLICY?

level: juniorimportance: should knowfreq 48%

answer

  1. two switches, not one
  2. no policy means no rows
  3. filters stay silent, inserts fail
  4. owner, superuser, BYPASSRLS exempt

basics

~20 s

Default deny applies: SELECT returns zero rows and UPDATE or DELETE touch zero rows without error, while INSERT fails with a row-level security error. Superusers, BYPASSRLS roles and, unless FORCE is set, the table owner are unaffected.

solid answer

~40 s

Row security has two parts: the table switch `ALTER TABLE ... ENABLE ROW LEVEL SECURITY` (stored in `pg_class.relrowsecurity`) and the policies made with `CREATE POLICY`. Policies on a table whose switch is off are ignored. The switch with no applicable policy gives **default deny**. Existing rows are filtered silently, so `SELECT` returns nothing and `UPDATE`/`DELETE` report 0 rows. New rows are *checked*, so `INSERT` fails with `new row violates row-level security policy for table "tasks"` (SQLSTATE `42501`). Table privileges from `GRANT` are still checked first, because policies only narrow rows. Superusers and `BYPASSRLS` roles are never affected, and neither is the owner unless `FORCE ROW LEVEL SECURITY` is set. `TRUNCATE` is outside row security, and `COPY FROM` is refused for any role the policies apply to.

code

sql · 14 lines
sql
CREATE TABLE tasks (
  id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  org_id uuid NOT NULL,
  title text NOT NULL
);
GRANT SELECT, INSERT, UPDATE, DELETE ON tasks TO app_rw;
ALTER TABLE tasks ENABLE ROW LEVEL SECURITY;

SET ROLE app_rw;
SELECT count(*) FROM tasks;              -- 0, no error
UPDATE tasks SET title = 'Renamed';      -- UPDATE 0
INSERT INTO tasks (org_id, title)
VALUES (gen_random_uuid(), 'Kickoff');   -- ERROR: new row violates row-level security policy for table "tasks"
RESET ROLE;

go deeper

for a junior

Recall that the table switch and the policies are separate, and what default deny does to each command: empty reads, zero-row updates and deletes, failing inserts.

for a middle

Explain filter versus check, why reads go quiet while inserts error with 42501, and which roles the switch never reaches.

for a senior

Show a safe rollout: policies first, switch in the same transaction, verification as the application role, then a catalog check of relrowsecurity and pg_policies.

for a principal

Treat default deny as the fail-closed property you are buying, and say where it stops: TRUNCATE, integrity checks and exempt roles still need privilege design.

## Two switches, not one PostgreSQL's **row-level security** (RLS) is configured in two places, and a policy does nothing until both are in place: - **The table switch.** `ALTER TABLE tasks ENABLE ROW LEVEL SECURITY` sets `pg_class.relrowsecurity` to true. Only the table's owner can run it (or a superuser). - **The policies.** `CREATE POLICY` attaches named predicates to the table. They are stored in the `pg_policy` catalog and shown readably in the `pg_policies` view. The two are independent. Policies can exist on a table whose switch is off. They are simply ignored, and every role with the right `GRANT` reads and changes every row. `ALTER TABLE ... DISABLE ROW LEVEL SECURITY` turns the switch off again **without dropping** any policy. That is why a table can look protected in `pg_policies` while enforcing nothing. ## Default deny, command by command This question is about the opposite case: the switch is on, and no policy applies to the current role and command. PostgreSQL then behaves as if one always-false policy existed, which the manual calls a **default-deny** policy. What the role sees depends on whether the command *filters* existing rows or *checks* new ones: | Command | What default deny does | What the client sees | |---|---|---| | `SELECT` | filters every existing row | 0 rows, no error | | `UPDATE` / `DELETE` | filters the rows they would touch | `UPDATE 0` / `DELETE 0`, no error | | `INSERT` | checks each new row against `false` | error: `new row violates row-level security policy for table "tasks"` | The error carries SQLSTATE `42501` (`insufficient_privilege`). The silent filtering on the read side is the trap. Take a multi-tenant project-management app that has just enabled RLS on `tasks`: reads do not crash, they show every customer an empty board. Default deny also works per command. If a table has only a `FOR SELECT` policy, that role is still denied `UPDATE`, `DELETE` and `INSERT`, because none of its policies covers those commands. ## Who is not affected The switch does not reach every role: - **Superusers** always bypass row security. - **Roles with the `BYPASSRLS` attribute** bypass it too. - **The table owner** bypasses it unless the owner has also run `ALTER TABLE ... FORCE ROW LEVEL SECURITY`. A developer who tests the new switch while connected as the owner or as a superuser sees every row and concludes nothing changed. Test as the role the application uses instead, for example with `SET ROLE app_rw` in psql. Row security never *grants* access either. The ordinary `GRANT SELECT, INSERT, UPDATE, DELETE ON tasks` checks run first, and a role without those privileges gets `permission denied for table tasks`, whatever the policies say. ## Statements that sit outside row security Some operations do not work row by row, so policies do not filter them: - `TRUNCATE` and the `REFERENCES` privilege apply to the whole table. A role with `TRUNCATE` on `tasks` can empty it regardless of RLS. - Referential-integrity and uniqueness checks always bypass policies. A duplicate-key error can therefore reveal that a hidden row exists. - `COPY tasks TO` applies the `SELECT` policies. `COPY tasks FROM` is not supported for a role the policies apply to: it fails with `COPY FROM not supported with row-level security` and a hint to use `INSERT` statements. ## Rolling it out without an outage The switch on its own denies everything, so the order of a rollout matters: 1. Create the policies first. While the switch is off they change nothing. 2. Enable the switch in the same transaction as the last policy. DDL is transactional in PostgreSQL, so no committed state ever has the switch on without its policies. 3. Verify as the application role, not as the owner: count rows per tenant, run one `INSERT` that should pass and one that should fail. 4. Check the catalog afterwards, as described in the next section. ## Reading the state Three places show the state: - `pg_class.relrowsecurity` says whether the switch is on. - `pg_class.relforcerowsecurity` says whether the owner is included. - `pg_policies` lists each policy's command (`cmd`), its `roles`, its `permissive` mode, and its `qual` and `with_check` expressions as text. A table with `relrowsecurity = true` and no rows in `pg_policies` is in default deny for every role that is not exempt.

  • If a table has only a FOR SELECT policy for a role, what happens to that role's INSERT?
    It fails with the row-level security error. No `INSERT` or `ALL` policy applies to the role, so the new row is checked against the default-deny predicate. Each command type needs its own applicable permissive policy, or an `ALL` policy, before the role can write rows.
  • How would you turn on row security for a busy table so the application never hits the default-deny window?
    Create every policy first, while the switch is still off and the policies change nothing. Then run `ENABLE ROW LEVEL SECURITY` in the same transaction as the last `CREATE POLICY`. Because DDL is transactional, other sessions see either no RLS or the full set, never the switch without its policies. Verify afterwards as the application role.

  • Enabling row-level security with no policies leaves the table fully readable
  • Default deny makes a SELECT raise a permission error
  • Disabling row-level security drops the table's policies
  • Row-level security policies replace the need for GRANT on the table
  • Testing as the table owner proves the policies work