Postgres: the person in a transaction setting
Plain Postgres has no `auth.uid()`, so the framework's permissions recipe has your api tell the database who is acting: it sets `app.user_id` at the start of every transaction, and the policies read it back. Row level security then repeats the app's permissions, and a trigger guards the workflow and four-eyes.
By Bohuslav PoláčekChecked
-- The api runs: select set_config('app.user_id', '<the person's id>', true);
-- at the start of every transaction it does for a person.
create function app_user_id() returns uuid
language sql stable
as $$ select nullif(current_setting('app.user_id', true), '')::uuid $$;
create table requests (
id uuid primary key default gen_random_uuid(),
title text not null,
status text not null default 'draft'
check (status in ('draft', 'submitted', 'approved', 'rejected')),
created_by uuid not null references users (id),
decided_by uuid references users (id),
decision_reason text,
-- a rejection always has a reason, whatever the api forgot (coalesce: a check
-- that evaluates to null passes, so a missing reason must count as empty)
check (status <> 'rejected' or coalesce(length(btrim(decision_reason)), 0) > 0)
);
alter table requests enable row level security;
-- the table's owner is bound by the policies too
alter table requests force row level security;
-- who may read: everybody signed in
create policy requests_select on requests for select
using (app_user_id() is not null);
-- who may create: a person, as themselves, as a draft
create policy requests_insert on requests for insert
with check (created_by = app_user_id() and status = 'draft');Where it plugs in
The starter never talks to Postgres. It talks HTTP to an api you write, and that api talks to Postgres:
- In the app: each module's seam,
src/modules/<module>/data/api.ts, switches to its HTTP adapter (data/api.http.ts) whenVITE_DATA_SOURCE=http, and calls plain JSON routes:GET /requests,POST /requests/movesand the rest, listed indocs/recipes/connect-your-data.md. - In your api: it validates the token, looks the person up, opens a transaction and runs
set_config('app.user_id', <id>, true)before anything else. - In the database:
app_user_id()reads that setting, the policies use it, andrequests_guard()refuses an illegal move or a decision on your own request.
The sketch is step 5 of docs/recipes/permissions.md. The Supabase example does the same job with auth.uid(); this one is for Postgres you run or rent yourself.
Steps
- Write the api that answers the routes of the requests contract, in whatever you like: Node, .NET, Go, Laravel.
- Connect it to Postgres as an ordinary role, not the owner and not a superuser. Keep the owner for migrations.
- Run the recipe's table,
enableandforce row level security, the policies and the guard trigger, adjusted to your names. - In every request: validate the token, start a transaction, call
set_configwithis_localtrue, do the change, write its history row, commit. - Set
VITE_DATA_SOURCE=httpandVITE_API_URL, rebuild, and run the module's contract test against a staging copy of the api.
What to watch
- Transaction, not session. With
is_localtrue the setting applies only to the current transaction. With false it stays for the rest of the session, and a connection pool hands that session to the next request. - Who is not bound. Superusers and roles with
BYPASSRLSalways bypass the policies, and the table's owner does too unless the table hasforce row level security. - Nobody set means nobody.
current_setting('app.user_id', true)answers null when the setting is missing, soapp_user_id()is null and the read policy lets nothing through. - Policies add up with OR. Two permissive policies for one command let through what either allows. Keep one policy per command and the state machine in the trigger, where the old and the new row are both in hand.
- A table with row level security and no policy shows nothing. A safe place to start.
Questions
Does the starter connect to Postgres directly?
No. The starter calls an HTTP api through its adapters, and the api talks to Postgres. No database driver ships in the browser, and no database credential either.
Why a transaction setting and not a user id in each query?
Because the policies then hold for every query the api runs, including one somebody forgot to filter. The setting is the one place the person enters the database, and it comes from the validated token.
Which database role should the api use?
An ordinary role that owns nothing and has no BYPASSRLS. Superusers, roles with BYPASSRLS and, without force row level security, the table's owner skip every policy.
How is this different from the Supabase example?
Supabase tells the database who is signed in through auth.uid(). Here your api does it with set_config at the start of each transaction. The policies and the guard trigger are the same idea.
Two ways
Build it yourself, or have it built
Build it yourself
Build your app on the framework
Get the codebase these apps stand on and build yours with Claude Code, Cursor or Codex.
One payment, lifetime updates
Get the frameworkHave it built
Have us build your app
Tell us how your team works. We plan the app with you and build it on the same framework at a fixed price.
After launch, Care keeps it improving, from €99 a month.
from €1,990
Describe your app