0

Postgres roles: anon, authenticated, service_role

Securityused in ~95% of apps

You have been picking a role from a button in this course's playground — anon, authenticated, service_role — as if it were a mode switch on a toy. It is not a toy and it is not simulated for your benefit: those are three ordinary Postgres roles, the same kind you'd create with `create role`, and every rule you already know about Postgres roles (GRANTs, ownership, attributes) applies to them exactly as written. The moment you see that, half of what feels like Supabase magic turns out to be plain Postgres you already understand.

Core idea. Supabase's API layer connects to your database as one shared login and, for every single request, runs `SET ROLE` to become one of three ordinary Postgres roles based on the caller's JWT: `anon` (no valid user token), `authenticated` (a valid user token — its `sub` claim becomes `auth.uid()`), or `service_role` (the secret key, created with the `BYPASSRLS` attribute, so Postgres never evaluates a policy for it at all). Nothing about these roles is exotic — they are plain `create role` statements, and their power comes entirely from ordinary GRANTs plus one ordinary role attribute.
Playground, this isn't a screenshot
request arrives with JWT role =
one shared login
authenticator
{ "role": "anon" }
set role anon;
per request
current_user is now
anon
rolbypassrlsfalse
anon sees: Only rows a policy admits for anon. auth.uid() is NULL — there is no signed-in user.
↑ play a little first, then dig in