# PostgreSQL database

HelixIAM keeps its state in one PostgreSQL database: realms, users, clients, keys, sessions and tokens (unless Redis
holds the sessions), and the schema history. This page covers which PostgreSQL versions are supported, where the JDBC
driver comes from, how the schema is migrated, and how to check what a running server sees.

## Supported versions

The server checks the database when it starts, once, before it serves anything.

| PostgreSQL | What the server does |
|---|---|
| 13 or older | Refuses to start, naming the version and the minimum (14). |
| 14 | Starts, with a warning: PostgreSQL 14 reached its end of life in November 2026. Plan the move to a newer version. |
| 15, 16, 17, 18 | Starts. These are the tested versions. |
| Newer than 18 | Starts, with a warning that the version is untested. With `HELIX_DATABASE_VERSION_STRICT=true` it refuses to start instead. |

The server also refuses a database of another product than the one it is configured for (`DB_VENDOR`, see below),
naming both. A warning is written to the log at start-up and shown in the [database health group](#check-the-database-and-the-driver).

The server and every database session run in UTC; nothing in the database's own time zone setting needs to change.

## Settings

| Variable | Default | Purpose |
|---|---|---|
| `DB_VENDOR` | *(required)* | The database product: `postgresql`. The Helm chart (`database.vendor`) and the Compose bundles set it. |
| `DB_HOST` / `DB_PORT` / `DB_NAME` | `localhost`, `5432`, `helixiam` | Where the database is. |
| `DB_URL` | empty | A whole JDBC URL instead of the three above, for driver settings that have no variable of their own (for example a client certificate: `jdbc:postgresql://db:5432/helixiam?sslcert=...&sslkey=...`). A parameter in the URL wins over the same `DB_*` setting. |
| `DB_USERNAME` / `DB_PASSWORD` | `helixiam` / empty | The database user. It owns the schema: it creates and alters tables when migrations run. |
| `DB_SSLMODE` / `DB_SSL_ROOT_CERT` | driver default | TLS to the database; see [Configuration](../getting-started/configuration.md#database). Production: `verify-full`. |
| `DB_DRIVER_DIR` | `/opt/helix/drivers` | The directory with the JDBC driver jar (below). |
| `HELIX_DATABASE_VERSION_STRICT` | `false` | `true` refuses an untested database version instead of starting with a warning. |
| `HELIX_MIGRATIONS_ENABLED` | `true` | `true`: the server migrates the schema at start-up. `false`: the migrations run elsewhere (below). |

The settings `spring.datasource.hikari.data-source-properties.*` and `spring.datasource.hikari.connection-init-sql`
are refused at start-up: they would bypass the checks the server makes on every new database session. Use the `DB_*`
variables or the query string of `DB_URL` instead.

## The JDBC driver

The server image carries no JDBC driver, and from 1.1.5 HelixIAM ships none. At start-up the server loads the one jar
it finds in `/opt/helix/drivers`, and only when that jar's SHA-256 is on the list of drivers the release was tested
with. The directory and the jar must be read-only for the server: a writable driver is refused. Nothing is downloaded
when the server starts, so an installation without internet access works the same way.

This release is tested with pgjdbc 42.7.14 (`postgresql-42.7.14.jar`, SHA-256
`73914527305a40cce504b0d3d90b23caf565136912d607ae8ae7c5895512332c`). How the jar gets there, with examples for Docker,
Podman, Helm, Compose, the Linux server, a Maven mirror, an air-gapped build and a CI pipeline:
[Your image: add the database driver](database-driver.md).

=== "Helm"

    Run your own image, the server image plus the driver, built from the release's `postgresql.Dockerfile`:

    | Value | Default | Purpose |
    |---|---|---|
    | `database.vendor` | `postgresql` | `DB_VENDOR`. |
    | `image.repository` / `image.tag` | the plain server image | Your image, for example `registry.example.com/acme/helixiam` and `v1.1.5-postgresql`. |
    | `database.driver.volume` | empty | Instead of your image: a volume source (`persistentVolumeClaim`, `hostPath`, `csi`, ...) that holds the driver jar and nothing else, mounted read-only over `/opt/helix/drivers`. Not a `configMap` or `secret`: their symlinks are refused. |

    With neither the server does not start, naming `helix.database.driver-dir`, and the install notes warn whenever
    `image.repository` is the plain server image (a path ending in `/helixiam/helix-iam-server`, on any registry).
    Give your own image another repository. `database.driver.image` was removed in 1.1.5: a value that still sets
    it fails the render ([upgrading from 1.1.4](database-driver.md#from-114-the-drivers-image)).

=== "Docker Compose (quick start)"

    The compose file builds the server image with the PostgreSQL driver itself: `docker compose up -d --build`
    (Compose v2.20 or later, BuildKit). `MAVEN_REPOSITORY` in `.env` names a Maven mirror for the download.

=== "One Linux server"

    The bundle ships an empty `drivers/` folder, mounted read-only (`./drivers:/opt/helix/drivers:ro`), and `.env` has
    `DB_VENDOR=postgresql`. Put the checked jar in it (`0444`, the folder `0555`), exactly one:
    [the steps](database-driver.md#6-linux-server-and-native-bundle). An upgrade keeps it.

## Schema migrations

Flyway builds and migrates the schema, from the migration folder of the configured database product
(`db/migration/postgresql`), never guessed from the connection URL. Every applied migration is recorded with its
checksum in `flyway_schema_history`. There is no other way to build the schema: the former `schema.sql` mode and
`HELIX_SQL_INIT_MODE` are gone (see [Upgrading](upgrades.md#multi-database)).

- **Default (`HELIX_MIGRATIONS_ENABLED=true`).** The first server to start applies the pending migrations; servers
  that start at the same time wait for it.
- **Migrations as a separate step (`HELIX_MIGRATIONS_ENABLED=false`).** The servers do not migrate, and a server
  refuses to start while a migration of its release is pending or failed, naming the versions. Run the migrations
  first with the same image and `HELIX_MIGRATE_AND_EXIT=true` (it migrates, then exits with 0). With Helm,
  `config.migrationsEnabled=false` does this for you: a pre-install/pre-upgrade hook Job,
  `<release>-helixiam-migrations`, runs before the servers start, and a failed migration fails the `helm` command (the
  Job's log stays). Its pod has the label `app.kubernetes.io/component: migrations`: a database-side network rule that
  admits only the server's pods must admit it too. The first install then needs the admin password in your Secret
  (`secrets.adminPasswordKey`).
- **Before the first pending migration** the server checks the data those migrations would refuse and stops, naming
  the rows, before anything changes. Correct the rows and start again.
- A plain Flyway command line cannot run the release's Java migrations: run the migrations with the server image.
- `DB_MIGRATION_SOCKET_TIMEOUT_SECONDS` (default `0`, no limit) bounds a read of the migrations; the application's own
  reads use `DB_SOCKET_TIMEOUT_SECONDS`.

## Check the database and the driver

The `database` health group shows what the server found at start-up: the database product, its version and support
level (with the warning, if any), and the loaded driver (file, version, SHA-256, and whether it is on the tested list).
It answers on the management port only (`HELIX_MANAGEMENT_PORT`, `8081` in the Helm chart and the Linux server bundle)
and is refused on the API port. It is not part of readiness.

```bash
kubectl exec deploy/helixiam -- curl -fsS http://127.0.0.1:8081/actuator/health/database
```
```json
{"status":"UP","components":{
  "databaseVersion":{"status":"UP","details":{"database.product":"PostgreSQL","database.version":"17.6","database.support":"tested"}},
  "jdbcDriver":{"status":"UP","details":{"driver.file":"postgresql-42.7.14.jar","driver.version":"42.7.14","driver.sha256":"7391...332c","driver.tested":true}}}}
```

The support detail is `tested`, `vendor_end_of_life` (PostgreSQL 14) or `untested`; the last two come with a
warning detail that says why.

## Moving from a `schema.sql` database

An installation that ran with `HELIX_MIGRATIONS_ENABLED=false` and `HELIX_SQL_INIT_MODE=always` has a database
without Flyway history. The new release refuses it until you start it once with `HELIX_FLYWAY_BASELINE_VERSION` set to
the version of the release that built the database. The server then proves the database is that version's schema
before it records the baseline and migrates. The steps, the release-to-version table and what to do when the
comparison finds differences are in [Upgrading](upgrades.md#multi-database).
