Getting Keycloak to use Postgres within docker compose

I am new to Keycloak, but I’m trying to set it up for a service I am creating. Below is a docker compose file for the (experimental) setup I am using. Some hostnames and passwords has been
modified :slight_smile:

When starting the configuration , I am able to log into Keycloak and it seems to work
normally. However, there is no shred of evidence that the Postgres database is being used.
In PyCharm, the database is available, but as you can see, the “public” bit of it, where all the tables should have been, is empty.

image

I have tried all the combinations of configurations I could think of, but none of them seems to make any difference with respect to use of Postgres.

All helps will be very greatly appreciated.

Best wishes
Bjørn

Content of my docker-compose.yml file:


version: '3.8'


services:

  postgres:
      image: postgres:15.5-alpine3.19
      volumes:
        - ./postgres_data:/var/lib/postgresql/data
      environment:
        POSTGRES_DB: keycloak
        POSTGRES_USER: keycloak
        POSTGRES_PASSWORD: password
      ports:
        - "5432:5432"
      networks:
        - backend

  auth:
    image: quay.io/keycloak/keycloak:23.0.3
    ports:
      - "8080:8080"
    environment:
      - KEYCLOAK_ADMIN=admin
      - KEYCLOAK_ADMIN_PASSWORD=verysecretpwd
      - KEYCLOAK_IMPORT=/tmp/realm.json
      - KC_PROXY=edge
      - KC_HOSTNAME=some-external-site.somewhere
      - PROXY_ADDRESS_FORWARDING=true
      - LOG_LEVEL=ALL
      - KEYCLOAK_IMPORT=/opt/keycloak/data/import/realm.json
      #Don't know how to connect to postgres database. NONE of these seems work:
      - KC_HEALTH_ENABLED=true
      - KC_METRICS_ENABLED=true
      - KC_DATABASE=postgres
      - KC_DB_VENDOR=POSTGRES
      - KC_DB_ADDR=postgres
      - KC_DB_DATABASE=keycloak
      - KC_DB_USER=keycloak
      - KC_DB_SCHEMA=public
      - KC_DB_PASSWORD=password
      - DATABASE=postgres
      - DB_VENDOR=POSTGRES
      - DB_ADDR=postgres
      - DB_DATABASE=keycloak
      - DB_USER=keycloak
      - DB_SCHEMA=public
      - DB_PASSWORD=password
    depends_on:
      - postgres
    command:
      - start
    volumes:
      - ./auth/import:/opt/keycloak/data/import
    networks:
      - backend

networks:
  backend:
    name: backend
    driver: bridge

volumes:
  postgres_data:
      driver: local

Where did you find these environment variables?

If you look at Running Keycloak in a container - Keycloak and Running Keycloak in a container - Keycloak you’ll see that the environment variables in question are

KC_DB=postgres
KC_DB_URL=<DBURL>
KC_DB_USERNAME=<DBUSERNAME>
KC_DB_PASSWORD=<DBPASSWORD>

Do yourself a favour and remove all (even non-database related) of you current variables and only add those which are valid for the quarkus distribution. They all start with KC, but some of those you defined do not exist.
Available options are documented here: All configuration - Keycloak
How to translate them into evironment variables is documented here: Configuring Keycloak - Keycloak

1 Like

Hi.

Thank you so much for taking the time to answer my question on this special day :-). I followed your advice. My config Keycloak now works was to database, and looks like this:

  auth:
    image: quay.io/keycloak/keycloak:23.0.3
    ports:
      - "8080:8080"
    environment:
      - KEYCLOAK_ADMIN=admin
      - KEYCLOAK_ADMIN_PASSWORD=aiaiaia
      - KC_HOSTNAME_ADMIN=localhost
      - KC_PROXY=edge
      - KC_HOSTNAME=some-machine.somewhere
      - KC_METRICS_ENABLED=true
      - KC_HEALTH_ENABLED=true
      - KC_DB=postgres
      - KC_DB_PASSWORD=password
      - KC_DB_SCHEMA=public
      - KC_DB_USERNAME=keycloak
      - KC_DB_URL_HOST=postgres
      - KC_DB_URL_DATABASE=keycloak
      - KC_LOG_LEVEL=ALL
    depends_on:
      - postgres
    command:
      - start
    volumes:
      - ./auth/import:/opt/keycloak/data/import
    networks:
      - backend

(to answer your question about where I found all those environment variables: In misc blogposts, some from reading from the docfile you pointed to, and some pure extrapolation. It turned out that your advice th R the FM was the exact thing I needed. Thank you again .-))

(Rmz)

2 Likes

I’ve spent days trying to figure this out, and this did it! Thanks!