Skip to content

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 EngineCloud
View as Markdown
On this page
  1. The roles
  2. Helpers for policies
  3. Patterns
  4. On by default

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

RoleWhoRLS
anona publishable key, nobody signed inapplies
authenticateda signed-in user, a PAT, an OAuth clientapplies
service_rolea secret key, a client with project:adminbypassed

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

FunctionReturns
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.

  1. The roles
  2. Helpers for policies
  3. Patterns
  4. On by default

    Search pages, sections and terms