# Row-level security (RLS)

Row-level security is a database feature that restricts, per user, which rows a query may return, insert, update or delete, on top of the ordinary table privileges. In PostgreSQL a table with row security enabled and no policy shows nobody anything, and Meadowstack uses it in its Supabase example so the database enforces the same rules as the interface.

Checked 2 December 2026. By Bohuslav Poláček. https://meadowstack.com/glossary/row-level-security/

*From the Supabase example: the signed-in person's role, read by every policy, and the team's two policies.*

From `docs/recipes/examples/supabase/0001_requests.sql` in the Meadowstack repository:

```sql
-- The signed-in person's role. Security definer so the policies below can
-- read it without the profiles policies calling themselves.
create function public.app_role() returns text
  language sql stable security definer set search_path = ''
  as $$ select role from public.profiles where id = auth.uid() $$;

create function public.app_name() returns text
  language sql stable security definer set search_path = ''
  as $$ select name from public.profiles where id = auth.uid() $$;

alter table public.profiles enable row level security;

-- Everyone signed in sees the team (names in the history, the team page).
create policy profiles_select on public.profiles for select
  using (auth.uid() is not null);

-- canWorkspace "team.role": an admin changes other people's roles, never their own.
create policy profiles_update on public.profiles for update
  using (public.app_role() = 'admin' and id <> auth.uid())
  with check (public.app_role() = 'admin' and id <> auth.uid());
```

## Why it matters in a business app

An app built quickly, by a person or a coding tool, tends to check permissions where it was asked to: on the button. The api then answers anyone who calls it directly. Row-level security puts the rule next to the data, so a forgotten check in the api returns nothing instead of another person's records.

It is only as good as the role the api connects with. PostgreSQL's documentation is explicit: superusers and roles with `BYPASSRLS` always bypass it, and a table's owner does too unless the table has `FORCE ROW LEVEL SECURITY`.

## How Meadowstack handles it

Meadowstack 2.1 is the front end, so it cannot switch RLS on for you. What it gives you:

- **The rules in one place to copy from.** `domain/permissions.ts` in each module ([ABAC](https://meadowstack.com/glossary/abac/) and [RBAC](https://meadowstack.com/glossary/rbac/) rules together), and `docs/recipes/permissions.md` with a sketch for PostgreSQL.
- **A worked example.** `docs/recipes/examples/supabase/0001_requests.sql` mirrors the starter's `canFor` and `canWorkspace` as policies, adds a trigger for the moves a policy cannot see (a policy cannot compare the old row with the new one) and writes the history in the same transaction. It is marked as an example to adapt, not shipped code, and nothing in the framework runs migrations.
- **The checklist.** `docs/security.md` asks before go-live that the api's database role is bound by RLS and that a test tries the forbidden calls directly.

Meadowstack 3.0, coming as an update to every licence, generates the database's row-level policies from one policy file. See also [Supabase for business apps](https://meadowstack.com/integrations/supabase/).

## Common mistakes

- **The api connects as the owner or a superuser.** The policies exist and apply to nobody.
- **RLS on, no policy, and a panic.** Default deny is the point: add the policies, do not switch it off.
- **Only `using`, no `with check`.** `using` filters the rows a command may touch; `with check` decides what it may write. A policy with only `using` reuses it for writes, which is not always what you meant.
- **A policy that calls a table with its own policy.** The example reads the role through a `security definer` function so the profiles' policy does not call itself.

## Forced on the table that matters

*Requests force row security, so even the table's owner is bound; the history is written by a trigger instead.*

From `docs/recipes/examples/supabase/0001_requests.sql` in the Meadowstack repository:

```sql
alter table public.requests enable row level security;
alter table public.requests force row level security;
-- Not forced on the history: the history trigger below writes it as the
-- table's owner, and people only ever get the two policies further down.
alter table public.request_events enable row level security;
```

## Questions

### Does row-level security replace permission checks in the api?

No. The api still checks, so it can answer with a clear reason; RLS is the layer that holds when the api forgets. The starter's simulated api asks the same can() before every change for that reason.

### Why can my admin script see every row?

Superusers and roles with BYPASSRLS always bypass row security, and a table's owner bypasses it unless the table forces it. Connect the app with a role that is bound by the policies.

### Does Meadowstack work without Supabase?

Yes. The rules are plain TypeScript and any backend can enforce them; the Supabase file is one worked example, and the permissions recipe sketches the same for any PostgreSQL.

## Sources

- [PostgreSQL documentation, Row Security Policies](https://www.postgresql.org/docs/current/ddl-rowsecurity.html), checked 2 December 2026

## Related

- [Permissions](https://meadowstack.com/patterns/permissions/) (Patterns)
- [Postgres: the person in a transaction setting](https://meadowstack.com/integrations/postgres/) (Integrations)
- [Supabase: the rules in the database](https://meadowstack.com/integrations/supabase/) (Integrations)
- [Multi-tenancy](https://meadowstack.com/glossary/multi-tenancy/) (Glossary)
- [From Bubble to an app in your own repository](https://meadowstack.com/from/bubble/) (Moving from)
- [Team and roles, with the rules in code](https://meadowstack.com/screens/roles-and-permissions/) (Screens)
