---
title: Database
description: The Postgres database a self-hosted Peal needs, and how its schema is kept up to date.
sidebar:
  icon: database
---

You give Peal one Postgres database, named by `DATABASE_URL`, and it keeps its data and its durable jobs there. You don't run migrations yourself: the server does.

## What the server does at startup [#database]

At startup, the server applies the database migrations it hasn't applied yet, one server at a time. It only starts listening once they're done.

Each part of Peal keeps its tables in a Postgres schema of its own: `assistant`, `auth`, `channels`, `legal`, `media`, `notifications` and `publishing`. The job runner's tables (`cluster_*`) are in `public`.

### Let it create `pg_trgm` [#pg-trgm]

The migrations create the `pg_trgm` extension, which the media library's search reads. It ships with PostgreSQL, and since it's a trusted extension, the database's owner may create it.

On a managed Postgres, you have two ways to make that work:

- Connect the server as the database's owner.
- Create the extension once beforehand, with `CREATE EXTENSION pg_trgm;`.

Its operator classes must also be reachable through the search path of the role that migrates. Some providers keep extensions in a schema of their own, such as `extensions`. Put that schema in the role's `search_path` before the server starts:

```sql
ALTER ROLE … SET search_path = "$user", public, extensions;
```

Without it, the migration stops with "operator class gin_trgm_ops does not exist".

### Let it read which cluster it runs on [#cluster-identity]

The media sweeps read which Postgres cluster the database runs on, to tell a restored database from the one it was ([Which database the folder belongs to](/self-hosting/storage#media-owner)). They call `pg_control_system()`, and read the timeline from `pg_walfile_name(pg_current_wal_lsn())`.

Stock PostgreSQL lets any role call them. A managed Postgres that refuses them leaves the two deleting sweeps failing at each run. They delete nothing, and the `peal-media-sweep-failing` alert fires.

A provider's clone or branch that keeps the system identifier and the timeline is caught only by the sweeps' epoch. Point a clone at a media prefix of its own.

## Connect it directly [#connections]

The server runs its durable jobs from the same database. It holds them with session advisory locks for as long as it runs.

> **Don't connect through a pooler in transaction mode**
>
> Connect the server to Postgres directly, or through a pooler in session mode.
> A pooler in transaction mode gives each statement another connection, and the
> server loses those locks.

These settings size the connections and the job records:

| Setting                  | Default   | What it does                                                                                                        |
| ------------------------ | --------- | ------------------------------------------------------------------------------------------------------------------- |
| `DATABASE_URL`           | none      | The Postgres database. Each server process opens up to `DATABASE_POOL_MAX` connections to it.                       |
| `DATABASE_POOL_MAX`      | `20`      | The connections each server process opens to it at most, for the API and the jobs.                                  |
| `AUTH_DB_POOL_MAX`       | `5`       | The connections each server process opens on top of those, for sign-in.                                             |
| `PEAL_CLUSTER_RETENTION` | `30 days` | A daily cleanup deletes the records of jobs that finished longer ago than this. Jobs still waiting are always kept. |

[Environment variables](/self-hosting/environment-variables#core) has the details of each, with `AUTH_DB_POOL_MAX` under [Sign-in](/self-hosting/environment-variables#sign-in).
