Skip to content

Managed PostgreSQL

MATVIS ships with a bundled PostgreSQL container (matvis-postgres) that holds MATVIS's configuration, event history and message cache. You can run that database on a PostgreSQL you operate instead: a managed service such as Amazon RDS, Azure Database for PostgreSQL Flexible Server or Google Cloud SQL, or a server of your own. The switch is configuration only: a few .env variables in Docker Compose, a few values in Helm. Nothing else in the stack changes.

The Identity database (keycloak-db in Compose, keycloak-postgres in Helm) is separate and stays bundled. This page is about the MATVIS database only.

What the database needs

Requirement Detail
Version PostgreSQL 13 or newer. MATVIS ships and tests with PostgreSQL 16.
Database and role One database and one login role. The role must own the database (or hold CREATE on schema public, which PostgreSQL 15 and newer no longer grant to every role) and owns every table the Configuration API creates. No superuser, no extension, no init script.
Database name Letters, digits and underscores (for example matvis). Some providers reject a hyphen in a database name.
Migrations The Configuration API runs its schema migrations on every start with the configured role, so the role keeps CREATE for the lifetime of the installation.
Connections A direct connection or session pooling. Transaction-mode poolers (PgBouncer in transaction mode, RDS Proxy, Azure's built-in PgBouncer in transaction mode) are not supported: two background tasks hold session advisory locks.
Network Reachable from the Configuration API over TCP on the configured port, with TLS as described below.

Connection budget

Each Configuration API worker (BACKGROUND_WORKERS) opens two connection pools, one per driver. The pool size and overflow apply to each pool, so the ceiling per worker is 2 x (DB_POOL_SIZE + DB_MAX_OVERFLOW). The cache cleanup process keeps one more pool, and the content purge and the encryption backfill hold one lock connection each while they run.

BACKGROUND_WORKERS DB_POOL_SIZE / DB_MAX_OVERFLOW Connections, about
4 (default) 5 / 10 (default) 135
4 2 / 2 40
2 2 / 2 20

Size the server's max_connections for one Configuration API replica with headroom for your own administration sessions. A small managed tier (50 connections) needs the smaller pool settings. Connections are opened on demand; the idle pool stays small.

Variables

In Docker Compose these are .env variables. In Helm you set the postgres.* values, which the chart turns into the same variables; four of them have no Helm value. Unset, every variable keeps the bundled database working as before.

Variable Helm value Default Meaning
DB_HOST postgres.dbHost matvis-postgres Server host name or address.
DB_PORT postgres.dbPort 5432 Server port.
DB_NAME, DB_USER, DB_PASSWORD postgres.dbName, postgres.dbUser, postgres.dbPassword or postgres.existingSecret required Database and login. The password may contain any characters.
DB_SSLMODE postgres.sslMode unset TLS mode, libpq's values: disable, allow, prefer, require, verify-ca, verify-full. Unset leaves the drivers' default: encrypted when the server offers it, certificate unchecked.
DB_SSLROOTCERT postgres.sslRootCert (Secret name and key) unset Path of the CA bundle inside the container, for verify-ca and verify-full.
DB_SSLCERT, DB_SSLKEY none, Compose only unset Client certificate and key, only for servers that require one.
DB_POOL_SIZE, DB_MAX_OVERFLOW postgres.poolSize, postgres.maxOverflow 5, 10 Per pool, see the connection budget.
DB_CONNECT_TIMEOUT none, Compose only 10 Seconds per connection attempt.
DB_WAIT_SECONDS none, Compose only 120 How long the Configuration API retries the database at startup before it exits with an error.

TLS

Use DB_SSLMODE=verify-full for a managed service: the connection is encrypted, the server certificate is checked against the CA you configure, and the host name is checked too. verify-ca checks the chain but not the host name. Every provider publishes its CA bundle (RDS: the regional rds-ca bundle; Azure: the DigiCert and Microsoft RSA root bundle; Cloud SQL: the instance's server CA, or the Cloud SQL Auth Proxy). Without DB_SSLROOTCERT, verify-ca and verify-full use the system CA store of the Configuration API image, which covers providers that chain to a public root.

In Docker Compose, copy the bundle into the db-certs/ directory next to the compose file; it is mounted read-only into the Configuration API at /etc/matvis/db-certs:

DB_SSLMODE=verify-full
DB_SSLROOTCERT=/etc/matvis/db-certs/ca.pem

In Helm, put the bundle in a Secret and name it in the values; the chart mounts it at /etc/matvis-db-certs and sets DB_SSLROOTCERT for you:

kubectl -n matvis create secret generic matvis-db-ca --from-file=ca.pem=<path-to-bundle>
postgres:
  sslMode: verify-full
  sslRootCert:
    secretName: matvis-db-ca
    key: ca.pem

A wrong CA or a host name the certificate does not carry is refused: the Configuration API's startup wait logs the driver's TLS error for every attempt and exits after DB_WAIT_SECONDS.

Docker Compose

The bundled database is the fallback: with BUNDLED_POSTGRES unset (every existing .env), the matvis-postgres container starts and the Configuration API uses it, exactly as before. To use your own server, set in .env:

BUNDLED_POSTGRES=0
DB_HOST=<your server>
DB_PORT=5432
DB_NAME=matvis
DB_USER=matvis
DB_PASSWORD=<password>
DB_SSLMODE=verify-full
DB_SSLROOTCERT=/etc/matvis/db-certs/ca.pem

BUNDLED_POSTGRES takes 0 or 1 only. With 0, docker compose up -d starts the stack without matvis-postgres; the Configuration API's dependency on it is optional, which needs Docker Compose 2.20 or newer (August 2023). The postgres-data volume of the bundled database is left untouched, so switching back is a matter of removing the line.

Starting state

The managed database starts empty: the Configuration API creates the schema on its first start. MATVIS does not move data from the bundled volume to a managed server.

Generated deployments

The deployment generator asks for the database and writes the settings for you:

Prompt Bundled (default) Managed
postgres bundled managed
db_host matvis-postgres your server's host name (required)
db_port 5432 your server's port
db_name, db_user matvis-db matvis (letters, digits and underscores suit every provider)
db_sslmode empty (driver default) verify-full
db_ca_file empty absolute path to your provider's CA bundle on the machine that generates

With managed, the generated .env holds BUNDLED_POSTGRES=0 and the DB_* values, and the setup script writes the CA bundle to db-certs/ca.pem on the server, so nothing is copied by hand. The generator stops with the reason, before writing anything, when a managed answer has no host, an unknown TLS mode or port, or a CA path that is relative, missing or not a certificate.

Helm

postgres:
  enabled: false                 # renders no PostgreSQL StatefulSet for MATVIS
  dbHost: db.example.internal
  dbPort: 5432
  dbName: matvis
  dbUser: matvis
  existingSecret: matvis-db      # key: dbPassword (or postgres.dbPassword in values.secrets.yaml)
  sslMode: verify-full
  sslRootCert:
    secretName: matvis-db-ca
    key: ca.pem
  poolSize: 5
  maxOverflow: 10

With postgres.enabled: false the chart renders no matvis-postgres StatefulSet, Service or Secret; the Configuration API's init container waits for dbHost:dbPort instead, its environment carries the DB_* variables above, and DB_PASSWORD comes from postgres.existingSecret. The install fails with a message when postgres.dbHost still names the bundled Service, when no password source is set, or when a CA Secret is named without verify-ca or verify-full. keycloak-postgres is unaffected.

Startup

Before migrating, the Configuration API waits for the database: one connection attempt every two seconds for DB_WAIT_SECONDS, each failure logged with host, port and the driver's error (never the credentials). When the time is up it logs one error naming the location and exits, so a wrong host or a TLS problem shows up in the container log as one clear line rather than a crash loop. With DB_HOST still at its default, the error also says that the bundled database runs unless BUNDLED_POSTGRES=0 and that a managed database needs DB_HOST.

Cloud notes

  • Amazon RDS: verify-full with the regional CA bundle. RDS Proxy runs in transaction mode and is not supported; connect to the instance.
  • Azure Database for PostgreSQL Flexible Server: verify-full with Microsoft's root bundle. The user name is the plain role name. Keep the built-in PgBouncer off for MATVIS, or use it in session mode.
  • Google Cloud SQL: either the public address with the instance's server CA and verify-full, or the Cloud SQL Auth Proxy as a sidecar with DB_HOST=127.0.0.1 and DB_SSLMODE unset (the proxy encrypts the hop to Cloud SQL).
  • Cloud IAM logins (RDS IAM tokens, Entra ID, Cloud SQL IAM) are not supported; use a static password.