# Row-level security

> How each request runs as a database role with the caller's claims, the helper functions your policies use, and patterns for users, teams and machines.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://data.docs.sypnia.com/data/row-level-security/

For agents: RLS in Sypnia Data: each Data API request is one transaction: SET LOCAL ROLE anon|authenticated|service_role|custom; set_config('request.jwt.claims', claims, true); set_config('request.sypnia.principal', …). Helpers: auth.uid() uuid, auth.role() text, auth.jwt() jsonb, auth.email() text, sypnia.client_id() text, sypnia.scopes() text[]. Same names as Supabase, so existing policies port. service_role bypasses RLS. Console creates tables with RLS on. Patterns: owner column, team membership via your own members table, machines via sypnia.client_id()/scopes().

The engine never decides which rows you may see: **Postgres does**, with row-level security (RLS). Every request through the Data API runs like this:

```sql
begin;
set local role authenticated;                              -- or anon, or service_role
select set_config('request.jwt.claims', '{"sub":"7c0e…","role":"authenticated",…}', true);
select set_config('request.sypnia.principal', 'user:7c0e…', true);
select … from public.orders …;                             -- your policies apply here
commit;
```

## The roles

| Role            | Who                                         | RLS          |
| --------------- | ------------------------------------------- | ------------ |
| `anon`          | a publishable key, nobody signed in         | applies      |
| `authenticated` | a signed-in user, a PAT, an OAuth client    | applies      |
| `service_role`  | a secret key, a client with `project:admin` | **bypassed** |

Custom roles are possible: a client or a user's `app_metadata.role` can name another role that `sypnia_authenticator` is a member of.

## Helpers for policies

| Function             | Returns                                           |
| -------------------- | ------------------------------------------------- |
| `auth.uid()`         | the user's id (`sub`) as `uuid`, or null          |
| `auth.role()`        | the role name                                     |
| `auth.jwt()`         | all the claims as `jsonb`                         |
| `auth.email()`       | the e-mail claim                                  |
| `sypnia.client_id()` | the OAuth client's id for machine tokens, or null |
| `sypnia.scopes()`    | the token's scopes as `text[]`                    |

The names under `auth` are the ones Supabase uses, so policies written for Supabase work unchanged.

## Patterns

```sql
-- each person sees and changes only their rows
create policy own on public.notes for all to authenticated
  using (owner = auth.uid()) with check (owner = auth.uid());

-- members of a team see the team's rows (members is your own table)
create policy team on public.projects for select to authenticated
  using (exists (select 1 from public.members m where m.team_id = projects.team_id and m.user_id = auth.uid()));

-- a machine with the right scope may insert
create policy worker_insert on public.invoices for insert to authenticated
  with check ('billing:write' = any (sypnia.scopes()));
```

## On by default

Tables created from the console have RLS turned on. The console and `GET /sql/v1/schema` flag every exposed table **without** RLS, and the MCP tool `describe_table` shows it too (`rls_enabled` and the policies), so an agent can warn you.
