# Enabling managed PostgreSQL on your deployment

Managed PostgreSQL is optional per deployment. A deployment that configures nothing has no managed PostgreSQL, and nothing about it fails: no errors, no retries, nothing offered to customers. This page is for the people who run a deployment. Customers should read [Managed PostgreSQL](/guides/databases/managed-postgresql).

## What you are switching on

Each database is a PostgreSQL cluster run by the [CloudNativePG](https://cloudnative-pg.io) operator in a region cluster, with its WAL and base backups written to an S3 bucket that **you** provide. The kernel creates and changes the clusters; you install the operator and its backup plugin, give the kernel the bucket and its keys, tell it which regions and images to sell, and run the two shippers that feed the Metrics and Logs tabs. Backups are not optional for a paid database, which is why the backup store is what switches the product on.

## 1. Install the operator and the backup plugin

In **every region cluster** where you want to sell it:

1. Install the CloudNativePG operator, **version 1.30.0 or later**, in the namespace `cnpg-system`. The kernel's bindings are generated for 1.30.1.
2. Install the barman-cloud plugin (`plugin-barman-cloud`), which provides the `objectstores.barmancloud.cnpg.io` resource. The kernel's bindings are generated for 0.15.1. Install it in the same namespace as the operator: the kernel finds both by the images of the deployments in `cnpg-system`.
3. Install cert-manager if the cluster does not have it yet, for the database certificate authority (section 3).

Every 30 minutes the kernel probes each region cluster and records what it found: the `clusters.postgresql.cnpg.io` resource, the operator deployment and its version, the backup plugin's resource and the Kubernetes version. A region is **usable** only when the operator is present and at least 1.30.0 and the plugin is installed. A region that fails is simply not offered, and staff see every problem at once, not only the first: operator resource missing, operator not found, version unreadable, version too old, plugin missing, or cluster unreachable.

Extensions that ship as their own image need the Kubernetes `ImageVolume` feature. The probe records whether it is available: Kubernetes 1.33 or later and operator 1.27 or later.

A region also needs one schedulable server per copy to sell high availability: a region with two schedulable servers can sell two copies, but refuses three.

## 2. Configure the kernel

Configuration is environment variables on the kernel. Secrets come from the deployment's Kubernetes Secret.

| Variable | Meaning |
|---|---|
| `MANAGED_POSTGRES_BACKUP_BUCKET` | **Required.** The bucket WAL and base backups go to. |
| `MANAGED_POSTGRES_BACKUP_ACCESS_KEY_ID` | **Required.** Secret. The access key id. |
| `MANAGED_POSTGRES_BACKUP_SECRET_ACCESS_KEY` | **Required.** Secret. The secret access key. |
| `MANAGED_POSTGRES_BACKUP_ENDPOINT` | Optional. The S3 endpoint of an S3-compatible store. Unset talks to AWS S3 itself. |
| `MANAGED_POSTGRES_BACKUP_REGION` | Optional. The S3 region, for AWS-style stores. S3-compatible stores need none. |
| `DATABASE_ROOT_CA_PEM` | Optional but recommended. The public certificate of the database root CA (section 3). |
| `SYSTEM_CA_BUNDLE_FILE` | Defaults to `/etc/ssl/certs/ca-certificates.crt`. The public bundle the kernel combines with the root into the `ca-bundle.pem` that customer containers get. |
| `UNIT_METRICS_STORE_URL` | The VictoriaMetrics the Metrics tab reads (section 5). |
| `UNIT_LOG_STORE_URL` | The VictoriaLogs the Logs tab reads (section 6). |

Rules:

- **The backup store is all or nothing.** Without a bucket, an access key id and a secret access key together, managed PostgreSQL is absent: the kernel boots normally and does not fail. The endpoint and the region never decide it.
- Use **one deployment-wide key pair**. Each database writes under its own prefix in the bucket, and customers never see the key.
- Backup storage is included in what customers buy, so it is yours to size and watch.
- The two metrics and logs URLs are shared with the units feature and are optional on their own. Without them the database still runs, but the Metrics and Logs tabs answer `501 capability-not-available`.

Roll the kernel to apply a change.

## 3. Install the database certificate authority

Every managed database requires TLS, and its certificate is issued by your own database CA, so a customer's client can verify it with one file. Without this step, databases keep the operator's own certificate and customer containers get no certificate mount, so the guide [Connect an app to a managed database](/guides/databases/connect-tls) does not hold as written.

1. Make **one root CA**, once, and keep its key offline.
2. Make **one intermediate per cluster** and install it in that cluster as the cert-manager `ClusterIssuer` named `zaroz-database-ca`. The kernel's repository has the runbook and the script for these steps (`scripts/database-ca`), including a verification that issues a short-lived test certificate and checks that it chains to the root.
3. Put the root's **public** certificate, as PEM, in `DATABASE_ROOT_CA_PEM`. It is a certificate and never a key, so it belongs in plain configuration, not in a Secret.

With the variable set, the kernel:

- issues each database's server certificate from the `zaroz-database-ca` issuer (90 days, renewed automatically);
- serves the root from the database's **Download certificate** button;
- mounts the certificate in every customer container at `/etc/zaroz/ca/`, and sets the environment variables the TLS guide describes.

Intermediates last 5 years and the root 10. To renew an intermediate, restore the root key, make a new one for that cluster and install it; clients keep working because they trust the root, not the intermediate.

## 4. Regions and images in admin

Staff manage the product in the admin app, which serves them under `/managed-databases`. Two things must exist before anything is for sale:

- **Regions.** Each region cluster has a row per engine with a switch (`enabled`), the **time zone** its maintenance windows are read in, and what the last probe found (operator version, features, when it ran and the error, if any). A region is sold only while the switch is on **and** the last probe found it usable.
- **Images.** The image table holds one row per PostgreSQL release: the major version (`18`), the patch, the image reference, the flavour, the operating system, an optional end-of-life date and a status. The reference must be pinned by digest (`name@sha256:` followed by 64 hex characters); a mutable tag is refused. The status is:
  - `current`: sold, and the target of minor updates;
  - `available`: not sold, but it keeps running on the databases that have it;
  - `deprecated`: its end of life is announced.

  Only majors with a `current` image are offered at purchase. Marking a new digest `current` makes the kernel update each database to it in that database's own maintenance window.

Staff also manage the **extension allowlist** there. Customers see exactly that list, for their major version.

## 5. Metrics: `vmagent-databases`

The kernel does not scrape databases. One **`vmagent-databases`** runs in each region cluster, in the platform namespace, and writes what it reads to VictoriaMetrics. In `deployments-kernel` it is `prod/monitoring/vmagent-databases.yaml`.

- It scrapes the CloudNativePG exporter on port `9187` of every instance pod, every 30 seconds, and the kubelet's volume and container figures for the database pods. It keeps an allowlist of series and drops the rest.
- The kernel labels every database pod with the service instance. A pod without that label is not scraped, and that label is how a customer can only ever see their own database.
- The kernel's network policy for each database admits exactly the pods labelled `app: vmagent-databases` in the platform namespace, on the exporter port. Keep that label on the agent.
- **Per region values:** `ZAROZ_CLUSTER`, the kernel's id for that cluster, and `REMOTE_WRITE_URL`, where the agent writes. In the kernel's own cluster the defaults are right; for another cluster, set both and write through an authenticated endpoint.
- The kernel reads the same store through `UNIT_METRICS_STORE_URL`.

Metrics start to appear within two minutes of a database starting. If you add a metric to the kernel's panels, add it to the agent's allowlist as well, or the chart reads as empty.

## 6. Logs: Vector

Database logs reach VictoriaLogs through the **Vector** DaemonSet (`prod/monitoring/vector.yaml` in `deployments-kernel`), which also ships unit logs.

- It tails only pods the kernel labelled as managed database pods, and the customer sees only the `postgres` container: the operator's, the plugin's and the pooler's lines never reach them.
- Which customer a line belongs to is decided from the labels the kernel and the operator set on the pod, never from anything the database prints.
- Each container is limited to 500 lines per second; lines above that are dropped, and a marker line records how many.
- **Environment of the DaemonSet:** `ZAROZ_CLUSTER`; `VECTOR_SELF_NODE_NAME`, from the downward API; `LOGS_WRITE_URL`, the VictoriaLogs write address; `VL_USER` and `VL_PASSWORD` when the write address needs basic authentication; and `VECTOR_DANGEROUSLY_ALLOW_ENV_VAR_INTERPOLATION=true`, which Vector needs to read the other variables from its configuration.
- After a pipeline change, restart the DaemonSet: its configuration is not renamed when it changes.
- The kernel reads the store through `UNIT_LOG_STORE_URL`. A configured store that cannot be reached answers log reads with `503 log-store-unavailable`.

The deployments repository also has `vmalert-rules-databases.yaml`, the rules that alert staff about database health.

## 7. Check it

1. Ask the kernel what it sells:

   ```bash
   curl -sS "$ZAROZ_API/managed-databases/availability" -H "$AUTH"
   ```

   Look for `available: true`, the region ids and the versions you expect.
2. If it is not available, read `unavailable`:

   | Code | What is missing |
   |---|---|
   | `not_configured` | The backup store variables, a bucket and a key pair together. |
   | `no_region` | No region is both switched on in admin and usable according to its last probe. Wait for the next probe (30 minutes) after you fix the cluster, or read the probe's error in admin. |
   | `no_image` | No image of the engine has the status `current`. |

3. Buy a test database in a region you enabled. It should reach **Ready**, show its metrics within two minutes and its log lines in **Logs**, and **Back up now** should finish. Then try a restore to the newest point.

## 8. What an unconfigured deployment looks like

- The kernel boots normally. The probe, the minor-update, the backup watch and the purge tasks are registered but return at once, with no error logs.
- `GET /managed-databases/availability` answers `404 capability-not-available` when no database engine at all is configured. When another engine is configured and PostgreSQL is not, its row reads `available: false` with `unavailable: ["not_configured"]` and lists no regions and no versions.
- No purchase is accepted. The console does not offer managed PostgreSQL, and the routes under `/service-instances/{id}/database` answer a clean `404 capability-not-available`, never a 500.
- Admin shows that managed PostgreSQL is not configured and names what is missing.
- The ten included shared databases of a subscription do not depend on any of this.

A region that is configured but failing is equally simple: it is not offered, and the reason is in admin.
