Why the database setting matters
Keycloak keeps everything durable in a relational database: realms, clients, users, credentials, sessions that must survive a restart, and the event log if you save events. Losing the database means losing every account. Pointing Keycloak at the wrong one, or at the right one without encryption, is a quiet risk that tends to surface at the worst moment.
The setting itself is small. Keycloak needs to know which database vendor you use and where the database is, plus a username and password. This tool builds those options, and the JDBC URL behind them, for PostgreSQL, MySQL, MariaDB, Microsoft SQL Server and Oracle.
How Keycloak 26 reads database options
The vendor goes in db, which is a build option. The location can be given in two ways.
- In parts:
db-url-host,db-url-port,db-url-databaseand, if needed,db-url-properties. Keycloak builds the JDBC URL for the vendor from them. - As one URL in
db-url, written in the driver’s own format. This is handy when you copy a URL from a cloud console or need a format the parts cannot express.
Credentials go in db-username and db-password. An optional db-schema picks a schema other than the default.
KC_DB=postgres
KC_DB_URL_HOST=db.internal.example.com
KC_DB_URL_DATABASE=keycloak
KC_DB_URL_PROPERTIES=?sslmode=verify-full
KC_DB_USERNAME=keycloak
KC_DB_PASSWORD=<from your secret store>The host above is a made-up sample. The same settings as one URL would be KC_DB_URL=jdbc:postgresql://db.internal.example.com:5432/keycloak?sslmode=verify-full.
db-url-properties includes its separator
Keycloak appends db-url-properties to the URL it builds, exactly as written. So the value starts with the separator the driver expects: ? for PostgreSQL, MySQL and MariaDB, and ; for SQL Server. Forgetting it produces a URL the driver cannot parse.
db is a build option
Changing db needs a rebuild when the server starts with --optimized. The location and credentials are runtime options and can change at every start.
JDBC URL formats by vendor
| Vendor | `db` value | Usual port | URL format |
|---|---|---|---|
| PostgreSQL | postgres | 5432 | jdbc:postgresql://host:5432/database |
| MySQL | mysql | 3306 | jdbc:mysql://host:3306/database |
| MariaDB | mariadb | 3306 | jdbc:mariadb://host:3306/database |
| SQL Server | mssql | 1433 | jdbc:sqlserver://host:1433;databaseName=database |
| Oracle | oracle | 1521 | jdbc:oracle:thin:@//host:1521/service |
Keycloak includes the drivers for PostgreSQL, MySQL, MariaDB and SQL Server. It does not ship the Oracle driver: add the Oracle JDBC driver JARs to the providers folder of your image yourself.
Encrypting the connection
Inside one host or a private network you may accept plain connections. Across networks, and on any managed cloud database, encrypt them and check the server’s certificate. Each driver spells this differently.
| Vendor | Encrypt | Encrypt and check the certificate |
|---|---|---|
| PostgreSQL | ?sslmode=require | ?sslmode=verify-full |
| MySQL | ?sslMode=REQUIRED | ?sslMode=VERIFY_IDENTITY |
| MariaDB | ?sslMode=trust (no check) | ?sslMode=verify-full |
| SQL Server | – | ;encrypt=true;trustServerCertificate=false |
Checking the certificate needs the database’s CA certificate where the driver can find it. For PostgreSQL that is usually a sslrootcert=/path/to/ca.crt property next to sslmode. Encryption without a certificate check stops eavesdropping but not a server that pretends to be yours.
Preparing the database
Keycloak creates and migrates its own tables, so it needs an empty database and a user that owns it. It does not need rights on any other database. On PostgreSQL that takes three statements, run as an administrator. The password here is a placeholder.
CREATE USER keycloak WITH PASSWORD 'change-me';
CREATE DATABASE keycloak OWNER keycloak;
REVOKE ALL ON DATABASE keycloak FROM PUBLIC;Use a long random password and store it where your deployment reads secrets. If you set db-schema, create that schema first and make the Keycloak user its owner.
Connection pool
Each Keycloak node keeps a pool of connections. The options db-pool-initial-size, db-pool-min-size and db-pool-max-size size it. Multiply the maximum by the number of nodes and keep the total under what the database allows, with room for backups, monitoring and an admin session. Check the defaults for your version with kc.sh start --help before you change them.
One database per Keycloak cluster
All nodes of one Keycloak cluster share one database. Two separate Keycloak clusters must never point at the same database, or they will overwrite each other’s data and caches will disagree.
What this tool does and does not do
- It builds the JDBC URL for the vendor from host, port, database or service name, and the TLS choice.
- It writes the Keycloak options in parts or as one
db-url, in command-line, environment, Compose, Kubernetes orkeycloak.confform. - It writes the password as
${DB_PASSWORD}, whichkeycloak.confresolves from the environment, so no secret ends up in the output. - It checks that the host, port and database name look valid.
- It does not connect to your database, create the database or user, or tune the connection pool.
How to use it
- Pick the database vendor. The port changes to that vendor’s usual one.
- Enter the host as a name or IP address, without a scheme, and the database name. For Oracle, enter the service name.
- Enter the username, and a schema if you do not use the default one.
- Choose how the connection is encrypted.
- Choose whether to write separate options or one JDBC URL.
- Copy the result in the format you deploy with, and put the real password in from your secret store.
Where it pays off
A first production deploy
Create a database and a user for Keycloak alone, give that user rights on its own database only, and point Keycloak at it. Keycloak creates its tables on first start. Do not share the database with your application.
Moving to a managed cloud database
Cloud databases usually require TLS and give you a CA bundle. Choose the encrypt-and-check option, add the CA path, and test the connection from the same network the Keycloak pods run in.
Moving between environments
Staging and production should differ only in host, database name and credentials. Keep db and the TLS choice the same, so a setting that works in staging works in production.
Upgrading Keycloak
Keycloak migrates its schema when a new version starts against an older database. Back up the database before the upgrade, and try the new version against a restored copy first.
Common errors and how to fix them
Connection refused or timed out
Keycloak cannot reach the host and port. Check the name resolves from inside the container or pod, the port is right, and a firewall or security group allows the connection.
Password authentication failed
The username or password is wrong, or the user has no right to connect to that database. With the official PostgreSQL image, the password is only set when the data volume is first created.
SSL or certificate errors
With verify-full or VERIFY_IDENTITY, the driver must trust the server’s CA and the host name must match the certificate. Use the name on the certificate, not an IP address, and give the driver the CA file.
No suitable driver found for Oracle
The Oracle driver is not in the image. Add its JARs to /opt/keycloak/providers and run kc.sh build.
Security notes
- Never put the database password in a Compose file, a Dockerfile or a command line. Inject it from a secret store.
- Give the Keycloak user rights only on its own database.
- Encrypt connections that leave the host, and check the certificate.
- Back up the database and test restores. It holds every credential in your realms.
Best practices
- Use the parts form for readability, and
db-urlonly when you need something the parts cannot express. - Keep the same vendor and major version in every environment.
- Watch the database’s connections and disk, not only Keycloak.
- Plan point-in-time recovery for production, not only nightly dumps.
If you would rather not run Keycloak and its database yourself, see pricing for our managed plans.