Roles, keys and row level security bypass
Map each key and each tool to the PostgreSQL role it runs as, understand why two of those roles bypass row level security, and tell a privilege error from a policy that hides rows.
Mental model#
A key says who calls, a PostgreSQL role says what the query may touch. The gateway turns each key, and each signed-in user, into one role; PostgreSQL then applies that role's privileges (GRANT) and the row level security policies written for it. Two roles skip the policies on purpose: they belong to code you run on a server.
anon key, no user ─▶ aura_anon privileges + RLS
anon key + user token ─▶ aura_authenticated privileges + RLS, auth.uid() = the user
service_role key (SDK) ─▶ aura_service_role bypasses RLS
service_role key (/raw) ─▶ tenant_<id>_admin bypasses RLS, owns the tables
direct connection ─▶ proj_xxxxxxxx privileges + RLSKeys#
| Key | Prefix | Where it may live | Becomes |
|---|---|---|---|
| anon | aura_anon_ | Browser, mobile app, anywhere | aura_anon; aura_authenticated when a user token is sent with it |
| service_role | aura_sk_ | Server, CI, your terminal | aura_service_role (SDK, REST), tenant_<id>_admin (/raw, schema API) |
| authenticated | aura_auth_; none by default, aura db api-keys create <name> --scope authenticated | Server | aura_authenticated, without a user (auth.uid() is NULL); see API keys |
Two initial keys, anon and service_role, are created with the project and shown once. Create, list and revoke keys with aura db api-keys or in the Studio under Settings › API keys. The gateway refuses a service_role key sent from a browser (401 SECRET_KEY_IN_BROWSER) or in a URL query string.
PostgreSQL roles of a project#
| Role | Login | Bypasses RLS | Used by |
|---|---|---|---|
aura_anon | no | no | Anon key without a user; aura db raw --as anon |
aura_authenticated | no | no | Signed-in users; aura db raw --as authenticated --user <uuid> |
aura_service_role | no | yes | service_role key on the SDK and REST path |
tenant_<id> | no | no | Base role of the project; the direct-connection role and the alias service_role inherit it |
tenant_<id>_admin | no | yes | service_role key on aura-db (/raw, the Studio SQL editor, schema API, migrations). Owns the tables it creates. |
aura_authenticator | yes | no | Connection role of aura-db and PostgREST, which switch to one of the roles above for each transaction |
proj_xxxxxxxx | yes | no | Direct connection (psql, ORMs). Inherits tenant_<id> and aura_authenticated, may SET ROLE tenant_<id>_admin, and may only open its project's database. |
anon, authenticated, service_role | no | no | Aliases kept for SQL written for other platforms: aura_anon, aura_authenticated and aura_service_role are members of them, so a grant or a policy written for anon also applies to aura_anon. The alias service_role itself does not have BYPASSRLS. |
<id> is the project id with underscores instead of dashes. The three aura_* roles and aura_authenticator exist once per cluster; the tenant_ and proj_ roles exist per project.
Tables created through Aurabase are granted to aura_anon, aura_authenticated and aura_service_role at creation, and protected by row level security, which Aurabase enables on them. Functions are not: PostgreSQL's default EXECUTE for everyone is revoked, so a function is callable only by the roles you grant it to.
Roles that bypass row level security#
aura_service_role and tenant_<id>_admin have the PostgreSQL attribute BYPASSRLS. This is intended: they serve the service_role key, which is meant for code you control on a server (an admin back office, a webhook, a migration, the Studio). Such code needs to read and write every row, for every user, without a policy written for it.
What keeps this safe is where the key lives, not the role:
- the key never goes to a browser: the gateway refuses it when the request has an
Originheader; - it never travels in a URL: the gateway refuses it in a query string;
- a request made with the key carries no user:
auth.uid()isNULL, so a policy can never mistake it for a user.
If a service_role key leaks, revoke it (aura db api-keys revoke <id>) and create another.
See it from your project#
aura db raw runs as the administrator role, unless --as asks for a public role. The query below returns the same answer for each of the three:
aura db raw "select current_user like 'tenant\_%\_admin' as admin_role, (select count(*) from todos) as todos"
aura db raw --as authenticated --user 00000000-0000-4000-8000-0000000000a1 "select current_user, (select count(*) from todos) as todos"
aura db raw --as anon "select current_user, (select count(*) from todos) as todos" admin_role │ todos
───────────┼──────
true │ 4
ℹ 1 row(s).
ℹ Executed as authenticated (00000000-0000-4000-8000-0000000000a1).
current_user │ todos
───────────────────┼──────
aura_authenticated │ 3
ℹ 1 row(s).
ℹ Executed as anon.
current_user │ todos
─────────────┼──────
aura_anon │ 0
ℹ 1 row(s).The administrator role counts every todo of the example schema; the demo user alice sees her three; anon sees none.
Privilege or policy#
PostgreSQL treats a missing privilege and a missing policy differently, and the SDK shows the difference:
| What you see | Cause | Fix |
|---|---|---|
data: [], no error, and a meta.hint | RLS is on and no policy lets the role read these rows. PostgreSQL hides rows instead of failing. | Add a select policy for the role. |
403 INSUFFICIENT_PRIVILEGE, new row violates row-level security policy for table "todos" | An insert or update that the with check of the policy rejects. | Write rows that the policy accepts (for an owner policy: your own user_id). |
403 INSUFFICIENT_PRIVILEGE, permission denied for function todo_count | The role has no EXECUTE on the function. | grant execute on function todo_count(uuid) to aura_authenticated; |
403 INSUFFICIENT_PRIVILEGE, permission denied for table … | The role has no privilege on the table: typically a table created over the direct connection. | Enable RLS, then grant, then add a policy (see Connect to your database). |
403 FORBIDDEN, Forbidden: `/raw` query is reserved to the service_role role | SQL sent with the anon key or a user token. | Run SQL from a server with the service_role key. |
The three INSUFFICIENT_PRIVILEGE cases share the PostgreSQL code 42501, so read the message:
import { createClient } from '@aurabase/aurabase-js'
const user = createClient(process.env.AURABASE_URL!, process.env.AURABASE_ANON_KEY!)
const email = `roles-${Date.now()}@example.com`
await user.auth.signUp({ email, password: 'correct-horse-battery-9' })
await user.auth.signInWithPassword({ email, password: 'correct-horse-battery-9' })
const notMine = await user.from('todos').insert({ user_id: '00000000-0000-4000-8000-0000000000a1', title: 'Not mine' })
console.log(notMine.error?.status, notMine.error?.code, notMine.error?.message)
const anon = createClient(process.env.AURABASE_URL!, process.env.AURABASE_ANON_KEY!)
const count = await anon.rpc('todo_count', { p_user: '00000000-0000-4000-8000-0000000000a1' })
console.log(count.error?.status, count.error?.code, count.error?.message)
const hidden = await anon.from('profiles').select('username')
console.log(hidden.data, hidden.error)403 INSUFFICIENT_PRIVILEGE new row violates row-level security policy for table "todos"
403 INSUFFICIENT_PRIVILEGE permission denied for function todo_count
[] nullThe script signs a test user up: on a project that requires email confirmation (the default), turn it off first, as in the Quickstart.
Trade-offs#
auth.role()differs by path. Through the SDK it returnsaura_anon,aura_authenticatedoraura_service_role; throughaura db raw --asit returnsanonorauthenticated. Target roles withto aura_authenticatedin a policy instead of testingauth.role().- The direct-connection role can become the administrator.
proj_xxxxxxxxmaySET ROLE tenant_<id>_admin, which bypasses RLS. Treat its password like theservice_rolekey. - A table created by the administrator role over the direct connection is open to the public roles until you enable row level security on it, because the hook that enables RLS only runs for SQL sent through Aurabase.
- No custom login role. A project cannot
CREATE ROLE(permission denied to create role); use policies onaura_authenticatedand claims such asauth.tenant()orauth.has_role()to model your own roles.
Related pages#
- Row-level security
Write the policies these roles are filtered by.
- How a request reaches Postgres
Where each role is set on each path, and where the claims come from.
- Connect to your database
Keys, the project URL, and the direct-connection role.
- Project isolation
How projects are kept apart, beyond roles.
- Roles and privileges
The privileges these roles hold, and how to reduce them.