# Row-level security

Source: https://cubibase.com/docs/database/row-level-security

Also available: [Markdown version](https://cubibase.com/docs/database/row-level-security.md) · [llms.txt](https://cubibase.com/llms.txt) · [full site content](https://cubibase.com/llms-full.txt)

Set a user column when you create a table and CubiBase writes the PostgreSQL policy for you: a row belongs to the user whose id it carries.

## The model

Every table created through CubiBase has Row Level Security enabled and forced. Two policies exist from the start, and the Policies screen lets you add, edit, or drop your own:

| Policy | Role | Effect |
| --- | --- | --- |
| Developer policy | `sql` (Console) | Full access, so you can manage every row |
| Isolation policy | `api` (Data API) | Only rows where the user column equals the caller’s id |

The user column must be a `text` column on the new table — `owner_id` in these docs. Its value is compared to the verified JWT subject, which the engine sets as `cubibase.user_id` for the transaction.

## The policy CubiBase creates

For a table `customers` with user column `owner_id`, the isolation policy is equivalent to:

**SQL · created for you**

```sql
CREATE POLICY customers_api_isolation ON public.customers
  FOR ALL TO example_project_db_api
  USING (owner_id = cubibase.current_user_id())
  WITH CHECK (owner_id = cubibase.current_user_id());
```

- `USING` limits which rows the user can read, update, or delete.
- `WITH CHECK` limits which rows the user can insert or update into: writing someone else’s id fails with `403 forbidden`.
- The check runs inside PostgreSQL, not in application code you have to remember.

## From JWT to rows

1. The user signs in; the access token’s `sub` claim is their user id.
2. Your app sends the token as `Authorization: Bearer …` on a Data API call.
3. The engine verifies the token and sets `cubibase.user_id` for that transaction.
4. PostgreSQL applies the policy: the query only sees rows where `owner_id` matches.

> **tip**: Insert rows with the user’s id in the user column. A common pattern is to read the id from the signup response (or the token’s `sub`) and set it on every write.

## What the policy does not do

- It is per-row ownership, not roles or teams. For richer rules — statuses, teams, published rows — write your own policies on the Policies screen.
- The user-column flag itself only exists at creation; an existing table can get an equivalent rule as a hand-written policy.
- It does not replace exposure switches: an unexposed operation is refused before any row is touched.
- Anonymous users work exactly like registered users — their id is real, so their rows are theirs.

## Additional resources

- [Policies](https://cubibase.com/docs/database/policies) Write your own USING and WITH CHECK rules.
- [Tables and columns](https://cubibase.com/docs/database/tables) Where the user column is chosen.
- [Anonymous users](https://cubibase.com/docs/auth/anonymous) Visitors get real ids, so their rows are protected too.
- [Security overview](https://cubibase.com/features/security) Keys, tokens, roles, and policies together.
