# Policies

Source: https://cubibase.com/docs/database/policies

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

The Policies screen manages Row Level Security on your tables: turn RLS on or off per table, and write your own PostgreSQL policies with USING and WITH CHECK expressions.

## How policies fit the role model

Every project has two query roles. The `api` role serves Data API requests and is subject to your policies. The `sql` role serves the Console (row browser, SQL editor) and normally has a permissive developer policy so you can manage every row.

| Target role | Who uses it | Typical policy |
| --- | --- | --- |
| `api` | Your application’s end users (Data API) | Restrict rows per user, per team, or per status |
| `sql` | You, in the Console | Full access for administration |

Tables created with a user column already carry an isolation policy for the `api` role. The Policies screen lists those policies alongside any you add, and lets you edit or drop them.

## Enabling and disabling RLS

- Each table row shows whether RLS is enabled and forced.
- Disabling RLS makes every grant effective for every row — the policies stay stored but stop being applied.
- FORCE RLS applies policies even to the table owner. CubiBase enables it with RLS so nothing slips past by ownership.

> **warning**: Turning RLS off on an exposed table removes row restrictions for every signed-in user. Keep it on unless the whole table is meant to be shared.

## Writing a policy

| Field | Meaning |
| --- | --- |
| Command | `ALL`, `SELECT`, `INSERT`, `UPDATE`, or `DELETE` — which statements the policy governs |
| `USING` | Which existing rows are visible for SELECT, UPDATE, DELETE |
| `WITH CHECK` | Which new or updated rows are allowed for INSERT, UPDATE |
| Permissive | Permissive policies combine with OR; restrictive policies combine with AND |
| Roles | `api`, `sql`, or both |

Expressions are ordinary PostgreSQL boolean expressions. Reference the signed-in user with `cubibase.current_user_id()`, which returns the verified JWT subject for the request:

**Example · published notes are readable by everyone**

```text
-- Command: SELECT · Role: api · USING:
status = 'published' OR owner_id = cubibase.current_user_id()
```

**Example · users may only write their own rows**

```text
-- Command: INSERT · Role: api · WITH CHECK:
owner_id = cubibase.current_user_id()
```

- INSERT policies accept only WITH CHECK; SELECT and DELETE accept only USING.
- For ALL and UPDATE, omitting WITH CHECK reuses the USING expression as the write check.
- Expressions must be a single expression — stacked statements are rejected with `400 validation_error`.
- Bare column names are checked against the table, so a typo fails fast instead of at query time.

## Editing and dropping

- Editing expressions or roles uses ALTER POLICY in place.
- Changing the command or the permissive flag recreates the policy inside one transaction, so the table is never left unprotected mid-change.
- Dropping a policy takes effect immediately for new requests.

> **tip**: Test a policy from your app with a real end-user token. The Console row browser uses the sql role, so it will not show you what the api role sees.

## Additional resources

- [Row-level security](https://cubibase.com/docs/database/row-level-security) The user-column policy created with each table.
- [Exposing tables](https://cubibase.com/docs/database/exposure) Grants decide the operation; policies decide the rows.
- [Views](https://cubibase.com/docs/database/views) Another way to shape what the API can read.
