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

Checked 2 December 2026. By Bohuslav Poláček. https://meadowstack.com/integrations/postgres/

*The recipe's sketch: who is acting, as a transaction setting, and the first policies that read it.*

From `docs/recipes/permissions.md` in the Meadowstack repository:

```sql
-- 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`) when `VITE_DATA_SOURCE=http`, and calls plain JSON routes: `GET /requests`, `POST /requests/moves` and the rest, listed in `docs/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, and `requests_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

1. Write the api that answers the routes of the requests contract, in whatever you like: Node, .NET, Go, Laravel.
2. Connect it to Postgres as an ordinary role, not the owner and not a superuser. Keep the owner for migrations.
3. Run the recipe's table, `enable` and `force row level security`, the policies and the guard trigger, adjusted to your names.
4. In every request: validate the token, start a transaction, call `set_config` with `is_local` true, do the change, write its history row, commit.
5. Set `VITE_DATA_SOURCE=http` and `VITE_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_local` true 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 `BYPASSRLS` always bypass the policies, and the table's owner does too unless the table has `force row level security`.
- **Nobody set means nobody.** `current_setting('app.user_id', true)` answers null when the setting is missing, so `app_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.

## Sources

- [PostgreSQL docs: configuration settings functions (set_config, current_setting)](https://www.postgresql.org/docs/current/functions-admin.html), checked 2 December 2026
- [PostgreSQL docs: row security policies](https://www.postgresql.org/docs/current/ddl-rowsecurity.html), checked 2 December 2026

## Related

- [Supabase: the rules in the database](https://meadowstack.com/integrations/supabase/) (Integrations)
- [Permissions](https://meadowstack.com/patterns/permissions/) (Patterns)
- [The approval flow](https://meadowstack.com/patterns/approval-flow/) (Patterns)
- [The audit trail](https://meadowstack.com/patterns/audit-trail/) (Patterns)
- [Roles and permissions UI patterns for business apps](https://meadowstack.com/blog/roles-and-permissions-ui-patterns/) (Blog)
- [A proof the customer approves in their own portal](https://meadowstack.com/screens/proof-approval/) (Screens)
