---
title: "Keep each customer's data apart · Agent Wranglers"
url: https://agentwranglers.se/playbook/data/schema-design/tenant-isolation
updated: 2026-10-20
licence: "All rights reserved"
---

# Keep each customer's data apart

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Recommended
- Chapter: [Schema design](https://agentwranglers.se/playbook/data/schema-design)
- Rule: When one database holds data for many customer accounts, tag every row with its account and let the database refuse rows from other accounts.
- How: Add a `NOT NULL` account column to every shared table, and include it in keys and indexes. In PostgreSQL, turn on row-level security with a policy that compares that column with a setting the app sets per request. Set it with `SET LOCAL` inside the transaction, so a pooled connection never carries it to the next request. Connect as a role that does not own the tables and has no `BYPASSRLS`, because owners and superusers skip the policies. `FORCE ROW LEVEL SECURITY` applies the policies to the owner too. App checks stay the first defence, as the [object access rule](https://agentwranglers.se/playbook/adversarial-testing/access-and-accounts/object-access) says.
- Check: Connect as the app's role with the setting for account A and select from every shared table. No row from account B comes back.

Example:

```sql
ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
ALTER TABLE invoices FORCE ROW LEVEL SECURITY;
CREATE POLICY invoices_by_account ON invoices
  USING (account_id = current_setting('app.account_id')::bigint);
-- Per request, inside the transaction
SET LOCAL app.account_id = '42';
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers data playbook: https://agentwranglers.se/playbook/data/schema-design/tenant-isolation`.

- Previous: [Plan how rows are removed](https://agentwranglers.se/playbook/data/schema-design/deletion-design)
- [Schema design](https://agentwranglers.se/playbook/data/schema-design)
