Skip to content
All writing

Data security · October 10, 2026 · 7 min

Put security in the database, not the UI

Per-user Postgres roles, row-level security, invariants in triggers. A UI bug should be embarrassing, not a breach.

Internal tools get the least security attention and hold the most sensitive data. That is not a cynical observation; it is a structural one. The people building them are usually in a hurry, the users are "trusted," and nobody writes a threat model for the weekly reporting app.

I built one of those apps this year: a weekly all-hands operating system for a fintech, where business units submit commitments, KPIs, risks, and asks, and leadership gets a Friday report deck. The data in it is exactly the kind you do not want one unit reading about another. So I made a decision up front that I now think should be the default for any multi-tenant application: the database enforces access. The application merely asks.

What that means concretely

Every request that touches user data runs as a dedicated Postgres role, not the application's superuser connection, inside a transaction that first sets the current user's ID as a session variable. Row-level security policies on every table then scope reads and writes by that user's unit and role.

create policy commitments_by_unit on commitments
  for all
  using (unit_id = current_unit_id())
  with check (unit_id = current_unit_id());

The application code for "list this unit's commitments" is select * from commitments. There is no where unit_id = ?. There cannot be a forgotten where unit_id = ?, because the policy supplies it whether the application remembers or not.

This is the whole point. A UI bug, a missed filter in a new endpoint, an over-eager admin screen: none of them can leak another unit's rows, because the rows were never visible to that connection in the first place.

Invariants go in triggers

Row-level security handles who can see what. The second half is what is allowed to happen, and I put that in the database too.

The app has rules that must never be bypassed: a unit cannot submit more than ten commitments in a week; a due date must fall inside the week it belongs to; once a week is frozen on Friday it cannot be edited; KPI attainment is computed one way, from one formula; the audit log is append-only.

Every one of those is a trigger or a constraint. The application also validates them, for better error messages, but if the application validation is wrong or skipped, the database says no. An append-only audit log enforced by a trigger that rejects updates and deletes is worth more than any amount of "be careful" in a code review.

The objection: it's harder to work with

It is, a bit. You have to think about roles. Migrations have to grant and revoke carefully. Your local development setup needs the same roles as production or you will be surprised. Testing requires actually running as the scoped role, not the superuser.

Against that, I would set the things you no longer have to do. You do not have to audit every query for a missing tenant filter. You do not have to worry that a new engineer's first endpoint is a data leak. You do not have to trust the ORM's notion of scoping. And when a security reviewer asks "how do you prevent cross-tenant access?", the answer is a policy they can read, not a tour of the codebase.

The same posture, elsewhere

Once you adopt it, the posture spreads.

In the identity platform I built, records are sealed with AES-256-GCM before they hit the database, so even a connection with full read access sees ciphertext. The audit log is append-only there too.

In the ops platform for a fintech team, sixteen migrations include RLS and function-grant hardening, because an exposed RPC function is just another way of bypassing a row filter.

In the agent-security API, every row carries a workspace ID and every query runs with the workspace in session, so a bug in one tenant's policy engine cannot read another tenant's agents.

The principle is the same everywhere: push enforcement as far down the stack as it can go. The UI is where bugs live. The database is where the data lives. Put the lock on the second one.

Where to start

If you have an existing multi-tenant app with filters in application code:

  1. Add a tenant column to every table that lacks one. Backfill it.
  2. Create a scoped role with no bypass privileges. Give the app that role for user-facing requests.
  3. Write policies one table at a time, starting with the most sensitive. Enable RLS, run your test suite as the scoped role, fix what breaks.
  4. Move your "must never happen" rules into constraints and triggers.
  5. Make the audit log append-only at the database level.

You will find at least one leak during step 3. Everyone does. That is the argument.

Written by

Samuel Ojerinde · AI Software Engineer

Next: The model is a dependency, not the architecture