---
title: "Turn on row-level security for every table · Agent Wranglers"
url: https://agentwranglers.se/playbook/data/multi-tenancy/row-level-security
id: DAT-83
level: Required
applies: client-db
risk: data
updated: 2026-10-27
build: a2a6bdfed830
tokens: 953
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Turn on row-level security for every table

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Multi-tenancy](https://agentwranglers.se/playbook/data/multi-tenancy.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-27.

- ID: DAT-83
- Level: Required
- Chapter: [Multi-tenancy](https://agentwranglers.se/playbook/data/multi-tenancy.md)
- Rule: When the browser talks to the database directly, turn on row-level security for every table and write a policy for each action.
- Check: A query lists public tables without row-level security, and it returns no rows. Using only the public key, read each table signed out, then as another user. Both return nothing.
- Risk: Data leaked or lost
- Applies when: The project lets the browser query the database directly.
- How: Builders often pair the app with a hosted database, such as Supabase, that the browser can query with a public key. That key is public by design, so only the database policies stand between strangers and your data. The secret `service_role` key skips every policy, so it never reaches the browser. [Keep each customer's data apart](https://agentwranglers.se/playbook/data/multi-tenancy/tenant-isolation.md) lists the roles that bypass policies. Write policies that compare the row's owner with the signed-in user. Insert and update policies need a `with check` clause too, or users can write rows for someone else. Do the same for file storage buckets. Each policy keeps customers apart as [Keep each customer's data apart](https://agentwranglers.se/playbook/data/multi-tenancy/tenant-isolation.md) says. Attack it as [Check ownership on every record](https://agentwranglers.se/playbook/accounts/roles-and-permissions/object-access.md) shows.

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### SQL example

```sql
alter table orders enable row level security;
create policy "owners read their orders" on orders
  for select to authenticated
  using ((select auth.uid()) = customer_id);
create policy "owners add their orders" on orders
  for insert to authenticated
  with check ((select auth.uid()) = customer_id);

-- Check: must return no rows
select tablename from pg_tables where schemaname = 'public' and not rowsecurity;
```

## Sources

- [OWASP Authorization Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Authorization_Cheat_Sheet.html)

## Related rules

- DAT-7 [Keep each customer's data apart](https://agentwranglers.se/playbook/data/multi-tenancy/tenant-isolation.md) in Data and databases
- ACC-77 [Check ownership on every record](https://agentwranglers.se/playbook/accounts/roles-and-permissions/object-access.md) in Accounts and identity
- TDD-11 [Test the access rules on the running product](https://agentwranglers.se/playbook/due-diligence/security-data-and-law/test-access-rules.md) in Technical due diligence

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/data/multi-tenancy/row-level-security">Agent Wranglers data and databases playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Keep each customer's data apart](https://agentwranglers.se/playbook/data/multi-tenancy/tenant-isolation.md)
- Next: [Run every background job inside its tenant](https://agentwranglers.se/playbook/data/multi-tenancy/tenant-context-in-jobs.md)
- [Multi-tenancy](https://agentwranglers.se/playbook/data/multi-tenancy.md)
