# Database

HelixIAM keeps everything in one SQL database: realms, users, applications, keys, sessions (with
`HELIX_SESSION_STORE=queue`) and the audit trail. It creates and migrates its own schema when it starts. Pick the
database first; the rest of the configuration does not depend on it.

| Database | Versions | For | Driver |
| --- | --- | --- | --- |
| **PostgreSQL** | 15, 16, 17, 18 (14 starts with a warning) | production; the default of the Helm chart and the Compose files | you add it to your image ([Your image: add the database driver](../operations/database-driver.md)) |
| **MySQL** | 8.4 (LTS); 8.0.19 or later starts with a warning | production | you add it to your image (licence: GPL; never shipped) |
| **H2** | the embedded 2.4.240 | evaluation only: the [standalone zip](quickstart.md) | shipped in the zip |

## Settings

The server reads the database settings from environment variables. Set them in the way of your install: Helm values,
the `.env` file of a Compose install, or the environment of the server's process.

| Variable | Helm value | Default | Meaning |
| --- | --- | --- | --- |
| `DB_VENDOR` | `database.vendor` | *(required)* | `postgresql`, `mysql` or `h2` |
| `DB_HOST` / `DB_PORT` / `DB_NAME` | `database.host` / `database.port` / `database.name` | `localhost`, `5432`, `helixiam` | where the database is (MySQL: port `3306`) |
| `DB_URL` | | empty | a whole JDBC URL instead of the three above |
| `DB_USERNAME` / `DB_PASSWORD` | the Secret's `db-username` / `db-password` | `helixiam` / empty | the database user; it owns the schema |
| `DB_SSLMODE` / `DB_SSL_ROOT_CERT` | `database.sslMode` / `database.caSecret` | the driver's default | TLS to the database; production: `verify-full` |
| `DB_ENCRYPTION` | the Secret's `db-encryption` | *(required)* | the key that encrypts secrets in the database (below) |
| `DB_DRIVER_DIR` | `database.driver` | `/opt/helix/drivers` | the folder with the JDBC driver (below) |

The [PostgreSQL page](../operations/database.md#settings) lists every setting, with time-outs and the connection
pool.

## PostgreSQL

Create a database and a user that owns it, then point the server at it:

```sql
CREATE USER helixiam WITH PASSWORD 'change-me';
CREATE DATABASE helixiam OWNER helixiam;
```

```bash
DB_VENDOR=postgresql
DB_HOST=db.internal
DB_NAME=helixiam
DB_USERNAME=helixiam
DB_PASSWORD=change-me
DB_SSLMODE=verify-full
DB_SSL_ROOT_CERT=/etc/helixiam/db-ca.pem
```

`DB_SSLMODE` takes `disable`, `allow`, `prefer`, `require`, `verify-ca` or `verify-full`. The driver's default,
`prefer`, encrypts when the database offers TLS but does not check its certificate: in production use `verify-full`
with `DB_SSL_ROOT_CERT`, the PEM file of the CA that signed the database's certificate. With Helm, put that file in
a Secret and name it in `database.caSecret`.

HelixIAM ships no driver: build your image with the release's `postgresql.Dockerfile` (pgjdbc 42.7.14, pinned by its
SHA-256), or put the jar in the `drivers/` folder of a Linux server bundle. The quick-start compose file builds it
for you. How, with examples: [Your image: add the database driver](../operations/database-driver.md). More on the
database: [PostgreSQL database](../operations/database.md).

## MySQL

The database must use the `utf8mb4` character set; the server refuses another one:

```sql
CREATE DATABASE helixiam CHARACTER SET utf8mb4;
CREATE USER 'helixiam'@'%' IDENTIFIED BY 'change-me';
GRANT ALL PRIVILEGES ON helixiam.* TO 'helixiam'@'%';
```

The settings are those of PostgreSQL with `DB_VENDOR=mysql` and `DB_PORT=3306`.

HelixIAM does not ship the MySQL driver (MySQL Connector/J is licensed under the GPL): download it yourself. The
server loads it only when its SHA-256 is the tested one:

| | |
| --- | --- |
| Driver | MySQL Connector/J 9.7.0 (`mysql-connector-j-9.7.0.jar`) |
| SHA-256 | `0353648eaa1c91e0f4020c959abf756bc866ffd583df22ae6b6f6e0cbd43eb44` |

```bash
curl -fLO https://repo1.maven.org/maven2/com/mysql/mysql-connector-j/9.7.0/mysql-connector-j-9.7.0.jar
echo "0353648eaa1c91e0f4020c959abf756bc866ffd583df22ae6b6f6e0cbd43eb44  mysql-connector-j-9.7.0.jar" | sha256sum -c
```

Build your image with the release's `mysql.Dockerfile`, which downloads and checks exactly this jar, or put the jar
in the `drivers/` folder of a Linux server bundle: [Your image: add the database driver](../operations/database-driver.md#7-mysql).
More: [MySQL database](../operations/database-mysql.md).

## H2 (evaluation only)

The [standalone zip](quickstart.md) runs H2 inside the server's process, in the folder `data/` next to `bin/`.
`bin/helixiam` sets everything: `DB_VENDOR=h2`, the file `data/helixiam`, and a generated database password and
encryption key in `data/helixiam.env`. Nothing to configure.

H2 is for an evaluation on one machine. The server refuses it with more than one replica
(`HELIX_CLUSTER_EXPECTED_REPLICAS` above 1), and the standalone server listens on `127.0.0.1` only. Its data does not
move to PostgreSQL or MySQL: for production, start on one of those.

## The driver folder

The server image carries no JDBC driver. The server loads **the one jar** in `DB_DRIVER_DIR`, and only when:

- the jar's SHA-256 is on the server's list of tested drivers for `DB_VENDOR` (a jar with another checksum is
  refused at start-up);
- neither the folder nor the jar can be written by the server's user (mount it read-only; the standalone launcher
  makes `drivers/` read-only itself, so it refuses to start as root or on a filesystem without permissions such as
  FAT or exFAT).

Nothing is downloaded when the server starts. The jar gets there in your own image (the server image plus the
driver) or in a read-only folder you mount: [Your image: add the database driver](../operations/database-driver.md).
The health group `/actuator/health/database` (on the management port) shows the driver in use and whether it is
tested (`jdbcDriver`).

## The encryption key

`DB_ENCRYPTION` encrypts the secrets in the database, such as realm signing keys and TOTP secrets. Use at least 32
hex characters (the server refuses a weak key):

```bash
openssl rand -hex 16
```

It is read **on every start** and must stay the same for the life of the database: with another key the stored
secrets cannot be read. Back it up separately from the database (see [Backup](../operations/backup.md)). The server refuses to
start without it (only the `dev` profile has a built-in demo key). To change it, rotate it with `DB_ENCRYPTION_NEXT`
as [Key custody](../operations/key-custody.md) describes; never just replace it.
