Skip to content

Instantly share code, notes, and snippets.

@mkuznyetsov
Last active July 24, 2019 12:56
Show Gist options
  • Save mkuznyetsov/457567a1bdaad0b843e088ba0b664ea5 to your computer and use it in GitHub Desktop.
Save mkuznyetsov/457567a1bdaad0b843e088ba0b664ea5 to your computer and use it in GitHub Desktop.
Migrating Keycloak database from H2 to PostgreSQL
Before commit 3d8ea835259e6b3e1fa00c35abca359acac0e74f in Eclipse Che repository, Keycloak database would be incorrectly initialized as default H2 database, ignoring the environment variables from Deployment.
Now, Keycloak will use the PostgreSQL, leaving H2 database unused.
In case it is needed to migrate your Keycloak realm from old H2 database to PostgreSQL, there are following steps:
1. Export from H2 Database.
Edit the Keycloak Deployment to perform an export of database to a persisted volume (in this case it’s “keycloak-data” volume which is located at “/opt/jboss/keycloak/standalone/data”).
Also make sure to set variable DB_VENDOR to H2.
```
"containers": [
{
"name": "keycloak",
"image": "eclipse/che-keycloak:nightly",
"command": [
"sh",
"-c",
"unset POSTGRES_PORT; /opt/jboss/docker-entrypoint.sh -Dkeycloak.migration.action=export -Dkeycloak.migration.provider=dir -Dkeycloak.migration.strategy=OVERWRITE_EXISTING -Dkeycloak.migration.dir=/opt/jboss/keycloak/standalone/data"
],
...
"env": [
{
"name": "DB_VENDOR",
"value": "H2"
},
...
```
After that, when pod will be recreated, Keycloak will export the data to the `/opt/jboss/keycloak/standalone/data` volume.
2. Import to PostgreSQL Database.
Now change deployment to connect to PostgreSQL DB and perform the import from previously exported DB with the following command:
```
"containers": [
{
"name": "keycloak",
"image": "eclipse/che-keycloak:nightly",
"command": [
"sh",
"-c",
"unset POSTGRES_PORT; /opt/jboss/docker-entrypoint.sh -Dkeycloak.migration.action=import -Dkeycloak.migration.provider=dir -Dkeycloak.migration.strategy=OVERWRITE_EXISTING -Dkeycloak.migration.dir=/opt/jboss/keycloak/standalone/data"
],
...
"env": [
{
"name": "DB_VENDOR",
"value": "POSTGRES"
},
...
```
3. Restore Keycloak deployment.
After the migration has been performed, remove the import command back to disable the migration process on next Keycloak startups:
```
"containers": [
{
"name": "keycloak",
"image": "eclipse/che-keycloak:nightly",
"command": [
"/scripts/kc_realm_user.sh"
],
...
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment