Skip to content
BENIBIX Learn

Row-Level Security (RLS)

Also known as RLS, Row Access Policy, Row-Level Access Control

One table, with rows hidden by person. Each reader sees only their own rows.

Think of it like

It is like a class list where each teacher sees only their own pupils. It is still one list.

Example

In PostgreSQL, a table with row security on and no policy shows no rows at all. Lucy's team adds one policy. Now each manager sees only their own accounts.

Why it matters

Without it, teams make one copy of a table per group. The copies drift apart, and any one of them can leak.

Definition

Row-level security lets people read one table but see different rows. A rule, called a policy, is set on the table. Each time someone reads it, the database adds a filter for them. A sales rep in Europe then sees only customers in Europe. Users cannot skip the filter. So one table can serve many teams.

Example code

sql
ALTER TABLE accounts ENABLE ROW LEVEL SECURITY;
CREATE POLICY own_accounts ON accounts
  USING (manager = current_user);
Each manager now sees only the rows that name them.
The greyed rows are row-level security at work. Lucy reads the same table and sees only Europe.

Common mistake

Testing it while logged in as the owner of the table. In PostgreSQL the owner skips the policy by default. So every row still shows.

Key takeaways

  1. 1
    Same table. One table, many views of it.
  2. 2
    Filtered on read. The database adds the filter each time.
  3. 3
    Hard to skip. Users cannot turn the filter off.