Using PostgreSQL storage

The deployment of Keycloak can use a PostgreSQL instance.

Enabling PostgreSQL storage

Using a PostgreSQL database is available to store data for the running Keycloak instances. It can be enabled via the following settings in the .env file in the provision/kubernetes folder.

Use the following settings to activate PostgreSQL:

KC_DATABASE=postgres

See Customizing the deployment for a list of all configuration options.

After every change, re-run the task command to apply the changes to the minikube instance. Task will trigger the necessary scripts and will use the minimal steps to update the minikube. Running rebuild.sh is not required: It will destroy the complete instance and will take a lot longer to complete.

The deployment adds a new PostgreSQL pod to the minikube setup and removes all other storage pods that are no longer necessary. Every time the pod is restarted, the database is cleared.

Keycloak connects to the PostgreSQL database pod inside minikube and populates the DB schema. Database metrics are available in Grafana. The SQL Pad allows accessing the database via the browser.

minikube runtime view postgres.dio

Verifying the setup

The automated script verifies that Keycloak starts up, which requires that PostgreSQL start up first.

Connecting a local application to PostgreSQL

PostgreSQL’s database port is also available as a node port so that it can be accessed from a local application. Assuming the command minikube ip returned 192.168.39.39, the JDBC URL is jdbc:postgresql://192.168.39.39:30009/keycloak.

minikube’s IP address changes every time the minikube instance is re-created.

It can be used, for example, to connect the developer’s IDE to the database.

The connection details: Port is always 30009, username is keycloak, password is pass, database name is keycloak.

All ports are specified without the port offset. If you are using the configuration option KC_PORT_OFFSET you need to add the offset to port numbers. For the port offset XX (00-26) and the port number 300YY the resulting port will be 3XXYY.

Connecting to the database on the command line

To start an SQL shell inside the PostgreSQL container, use the following command:

kubectl exec `kubectl get pods --selector=app=postgres -n keycloak -o name` -n keycloak -it -- psql --user keycloak

Metrics from the PostgresSQL database

There is an instance of postgres_exporter running in minikube and its metrics are collected in Prometheus.

Additional SQL query for metrics can be defined in pgexporter-queries.yaml.

Metrics and Dashboards for PostgreSQL

Metrics from the PostgreSQL are available in Prometheus. The metrics are shown in the standard Dashboard “keycloak-perf-tests”