# How your database is built

Under **Data** and **Security** the console has a page for each part of your project's section of the database. Some show what is there; on others you make and remove things without writing SQL.

- Everything listed belongs to your project's own schema. Another project's tables, functions and roles are never shown.
- What you make on these pages is made as your project's own database role, and stays inside the project.
- Anything your own [migrations](https://rezincore.com/docs/database/sql.md#migrations) made is marked **From a migration** and is changed there, not here.
- A member whose role is read-only can look but not change. Every change is on the project's [record](https://rezincore.com/docs/usage-and-logs.md#logs).

## Diagram

**Data → Diagram** draws every table with its columns and a line for each link between tables (a foreign key). It fills the window; scroll inside it for a large schema. Click a table's name to open its rows.

## Database functions

**Data → Database functions** lists the functions in your schema: name, what each takes and returns, the language, and whether it runs as the caller or as its owner. A function that runs as its owner is marked, because it can see rows the caller could not: check that it is meant to.

**New database function** makes one:

| Field | What to enter |
|---|---|
| Name | Lower-case letters, digits and underscores |
| It takes | Each argument's name and type, with a comma between, for example `order_id uuid, tax numeric default 0`. Leave empty for none |
| It returns | A type, for example `numeric`, `boolean`, or `trigger` for a function a trigger will run |
| Written in | SQL or PL/pgSQL |
| What it does | The body only, for example `select sum(price) from order_lines where order_id = $1` |

A function made here always runs with the reach of **whoever calls it**, never more, and looks up names in your project's schema first. **Change** replaces its body; **Delete** removes it.

These are functions inside the database, written in SQL. They are not the same as [Functions](https://rezincore.com/docs/functions.md), which are your own server-side code.

## Triggers

**Data → Triggers** lists each trigger with its table, when it fires (before or after an insert, update or delete) and the function it runs.

**New trigger** asks for the table, a name, before or after, which of "added", "changed" and "deleted" it fires on, and the function to run. The function must be one of your own that returns `trigger`; make it on the Database functions page first. A trigger made here runs once for each row.

Rezin Core adds its own triggers to a table when you switch on [realtime](https://rezincore.com/docs/realtime.md) or a [webhook](https://rezincore.com/docs/webhooks.md) for it. They are listed with the rest, marked as Rezin Core's own, and are switched off on the table's or the webhook's own page, not here. Names beginning `rezin_` are kept for them.

## Value lists

**Data → Value lists** shows your enumerated types: a named list of allowed values that a column can be limited to, such as `draft, sent, paid`.

- **New value list** takes a name and the values, with commas between.
- **Add** puts another value on the end of a list.
- A value cannot be removed or renamed once added. That is how the database works.
- **Delete list** removes the whole list, and is refused while a column still uses it.

## Indexes

**Data → Indexes** lists every index with its table and definition. Add or remove one on the table's **Structure** tab in the [table editor](https://rezincore.com/docs/database/tables.md#changing-a-table).

## Extensions

**Data → Extensions** lists common Postgres extensions and whether each is switched on for this server. Extensions are part of the database server and shared by every project, so a project cannot switch one on by itself. If you need one that is not on, ask Rezin Core.

## Publishing

**Data → Publishing** shows, for each table, what it tells the outside world when its rows change:

- **Live updates**: whether signed-in listeners hear changes to the rows they may read. Tick or untick it here; it is the same switch as on the table's own page. A table needs a key column first. See [Realtime](https://rezincore.com/docs/realtime.md).
- **Webhooks**: each webhook set on the table, whether it is on, which changes it sends and where to. Add or change one on the [Webhooks](https://rezincore.com/docs/webhooks.md) page.

There is one list. It is not a second setting that could disagree with the other pages.

## Backups

**Data → Backups** lists the backups that include your project. Backups cover the whole server, are sealed, and are taken every night; the newest is restored into a scratch database as a test, and the page shows whether that passed. See [Security and privacy model](https://rezincore.com/docs/security.md#backups) for what is and is not promised.

## Change history

**Data → Change history** lists the migrations applied to your project, newest first, with when each was applied. Apply one with `rezin migrations push` or the connector's `apply_migration`; see [SQL and migrations](https://rezincore.com/docs/database/sql.md#migrations).

## Database roles

**Security → Database roles** shows the two database roles that belong to your project, how many connections each has open, and what each may do to every table (read, add, change, delete):

| Role | What uses it |
|---|---|
| `…_app` | Your application, through the API. Row-level security applies to it |
| `…_sql` | The SQL workspace, the table editor, the command-line tool and connected assistants. It reaches all of this project's rows and nothing outside the project |

What the application's role may do to a table, and which rows it gets, is changed under [Access rules](https://rezincore.com/docs/database/row-level-security.md). Time limits and the number of connections are on [Database settings](https://rezincore.com/docs/database/settings.md).

## Not built

- Sending a copy of your tables to an outside analytics service ("pipelines").
- Downloading a backup of one project, or restoring one project by itself. Backups cover the whole server; ask Rezin Core if you need data brought back.

## Not available

Connecting to the database directly with a Postgres client is not offered. A project can have a database login ready for when it is: see [Database settings](https://rezincore.com/docs/database/settings.md#the-project-s-database-login). Your application reaches its data through the [data API](https://rezincore.com/docs/database/data-api.md); you and your tools reach it through the console, the [command-line tool](https://rezincore.com/docs/tools/cli.md) and the [connector](https://rezincore.com/docs/tools/connector.md).
