Skip to content

Keycloak database URL builder

Build the KC_DB settings and the JDBC URL for Keycloak on PostgreSQL, MySQL, MariaDB, SQL Server or Oracle, with TLS to the database.

Runs in your browser. Nothing you paste is sent to us.

Database

A made-up host is filled in.

Write it as

Keycloak settings

JDBC URL
jdbc:postgresql://db.internal.example.com:5432/keycloak?sslmode=verify-full
Configuration
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='${DB_PASSWORD}'
The password is written as ${DB_PASSWORD}: in keycloak.conf, Keycloak reads it from that environment variable. In the other forms, put the real value in from your secret store.
With db-url-host and the other parts, Keycloak builds the URL for the vendor itself. db-url-properties is added to its end as written, separator included.
verify-full needs the database’s CA certificate where the driver can find it, for example with sslrootcert=/path/to/ca.crt added to the properties.

How to use it

  1. Pick the database and enter its host, port and name.
  2. Choose how the connection is encrypted.
  3. Choose separate host and database options or one JDBC URL.
  4. Copy the options in the format you deploy with.

What to know

Keycloak needs to know the database vendor (db) and where it is. Either give db-url-host, db-url-port and db-url-database and let Keycloak build the URL, or write the full JDBC URL in db-url.

db-url-properties is added to the end of the URL Keycloak builds, exactly as written, so it starts with the separator the driver expects: ? for PostgreSQL and MySQL, ; for SQL Server.

Keep the database password out of files. In keycloak.conf, ${DB_PASSWORD} reads it from an environment variable.

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-database and, 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.

PostgreSQL in parts, as environment variables
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` valueUsual portURL format
PostgreSQLpostgres5432jdbc:postgresql://host:5432/database
MySQLmysql3306jdbc:mysql://host:3306/database
MariaDBmariadb3306jdbc:mariadb://host:3306/database
SQL Servermssql1433jdbc:sqlserver://host:1433;databaseName=database
Oracleoracle1521jdbc: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.

VendorEncryptEncrypt 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.

PostgreSQL: a database and user for Keycloak
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 or keycloak.conf form.
  • It writes the password as ${DB_PASSWORD}, which keycloak.conf resolves 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

  1. Pick the database vendor. The port changes to that vendor’s usual one.
  2. Enter the host as a name or IP address, without a scheme, and the database name. For Oracle, enter the service name.
  3. Enter the username, and a schema if you do not use the default one.
  4. Choose how the connection is encrypted.
  5. Choose whether to write separate options or one JDBC URL.
  6. 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-url only 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.

Questions

Frequently asked questions

What is the JDBC URL for Keycloak with PostgreSQL?

`jdbc:postgresql://<host>:5432/<database>`, set as `db-url` or `KC_DB_URL`, with `db=postgres`. You can also give the host and database separately with `db-url-host` and `db-url-database`.

Which databases does Keycloak support?

PostgreSQL, MySQL, MariaDB, Microsoft SQL Server and Oracle, plus a development database for trying it out. Check the supported versions in the documentation for your Keycloak release.

How do I connect Keycloak to PostgreSQL over SSL?

Add `?sslmode=verify-full` to the URL or to `db-url-properties`, and give the driver the CA certificate, for example with `sslrootcert`. `sslmode=require` encrypts without checking the certificate.

What is db-url-properties in Keycloak?

Extra driver properties appended to the URL Keycloak builds from the parts. The value includes its separator, such as `?sslmode=require` for PostgreSQL or `;encrypt=true` for SQL Server.

Does Keycloak include the Oracle JDBC driver?

No. Download the Oracle JDBC driver, add the JARs to the `providers` folder of your image and run `kc.sh build`.

Should I use db-url or db-url-host?

Either works. The parts are easier to read and change per environment. Use `db-url` when you need a URL format the parts cannot produce.

Can Keycloak share a database with my application?

It is better not to. Give Keycloak its own database and user, so upgrades, backups and permissions stay separate from your application’s.

How many database connections does Keycloak open?

Each node keeps a pool sized by `db-pool-min-size` and `db-pool-max-size`. The total is roughly the maximum times the number of nodes. Make sure the database allows that many connections, plus room for other clients.

How do I keep the database password out of keycloak.conf?

Write `db-password=${DB_PASSWORD}` and set the `DB_PASSWORD` environment variable from your secret store. Or set `KC_DB_PASSWORD` directly as an environment variable.

Rather not run Keycloak yourself?

We run it for you on a cluster of your own, with backups, upgrades and written support. Starter and Premium begin with a 7-day trial.