Skip to content
Prompt Words
History Member

Row-level security (RLS)

Also called: RLS.

The database decides which rows each query may see, using a policy on the table such as "fund_id must match this request's fund". The app can run SELECT * FROM deals with no WHERE and still get only its own fund's rows, because Postgres checks the policy on every row. It is a second lock, not a replacement for the app's own checks: superusers skip it, the table's owner skips it unless the table uses FORCE, and if the app sets the wrong fund the policy returns that fund's rows.

Ana lists deals: the app sets her fund and runs a query with no WHERE. Ben lists deals does the same for Fund B. Turn RLS off shows what that missing WHERE leaks (press it again to turn RLS back on).

Rows in table: 6 · Rows returned: 0 · RLS on

Fund A and Fund B share one deals table. The app's query has no WHERE; a policy decides which rows each user gets.

    RLS on

    Say it in a prompt

    Turn on row-level security for the Postgres deals table, with one policy: USING (fund_id = current_setting('app.fund_id', true)) and the same WITH CHECK, so a user can neither read nor write another fund's rows, and a missing setting gives no rows instead of an error. If fund_id is a uuid, use NULLIF(current_setting('app.fund_id', true), '')::uuid instead: on a pooled connection that has used the setting before, an unset value reads as '' rather than NULL. Add an index on deals(fund_id). The app connects as app_user, which is not the table's owner and has no BYPASSRLS; also run ALTER TABLE deals FORCE ROW LEVEL SECURITY. Each request opens a transaction and runs SET LOCAL app.fund_id to the signed-in user's fund before any query, so COMMIT clears it before the connection goes back to the pool. Keep the app's own permission checks. Add a test that Ana (Fund A) gets 3 of the 6 seeded deals.

    Vague vs precise prompt

    Vague prompt

    make sure each fund only sees its own deals

    Typical resultAdds WHERE fund_id = ? to the deals queries in the app. Every new query, report and script has to remember it, the one that forgets returns every fund's deals, and nothing in the database stops it.

    Precise prompt

    Enable row-level security on deals with a policy USING (fund_id = current_setting('app.fund_id', true)) and the same WITH CHECK, and index deals(fund_id). Connect as app_user (not the owner, no BYPASSRLS), FORCE ROW LEVEL SECURITY, and SET LOCAL app.fund_id inside each request's transaction. Keep the app's own checks too.

    Typical resultA query that forgets its WHERE still returns only the current fund's rows, and writes to another fund's rows fail. The app still checks permissions itself, and a test proves Ana sees 3 of the 6 deals.

    Seen on

    • PostgreSQL docs: With row security on, every query on the table must pass a policy, and with no policy no rows are visible. Superusers and roles with BYPASSRLS always skip it; the table's owner does too, unless the table is set to FORCE ROW LEVEL SECURITY.
    • PostgreSQL docs: A policy's USING expression is added to queries on the table. Rows where it is not true are left out silently, with no error.
    • PostgreSQL docs: current_setting() reads a setting's value; with missing_ok set to true, a missing setting gives NULL instead of an error. set_config() with is_local set to true changes a setting only for the current transaction.
    • PostgreSQL docs: SET LOCAL lasts only until the end of the current transaction, committed or not. Outside a transaction it only gives a warning and does nothing.
    • PostgreSQL docs: Postgres accepts a setting with a two-part name, like app.fund_id, even when no extension defines it.
    • Supabase docs: Enable RLS on every table in an exposed schema, and think of a policy as a WHERE clause added to every query. The secret key uses a role with BYPASSRLS, so it must never reach the browser.

    You might describe it as

    • let the database hide other customers' rows
    • a filter on the table that every query has to pass
    • one fund must never see another fund's data, even if the app forgets a WHERE

    Not to be confused with

    • Relationship-based access (ReBAC)

      ReBAC answers "may this user do this to this object?" from stored relationships, for any service; row-level security filters the rows of one table inside the database, by a rule on each row.