Docker with an appliction postgres database and keycloak database: "The connection attempt failed."

Hello,

I trying docker with docker-compose. The first things were going smoothly but now I’m running into trouble, which I can’t solved till now. I don’t know if it is a Docker-compose issue or Keycloak isssue.

I have made a docker-compose with a services: postgres database and PGAdmin. This is working fine.
I have also made a docker-compose with a service with keycloak. Running this, than I have access to keycloak with my browser. Individually these docker-compose itself are running and working fine.

Now I try to combine these two docker-compose file, to one docker-compose.
My expectation is that will get one server “Docker Postgres employee_DB” with two databases:

  • employee
  • keycloak
  • and that I have access to Keycloak (localhost:8180)
  • and that I have access to PGAdmin (localhost:5050, and that I see two databases with there own schema “public”)
  • The realm is imported as well, with the users

But this is the error in the console:

custom_keycloak | 2024-03-24 11:16:00,092 WARN [io.agroal.pool] (agroal-11) Datasource ‘’: The connection attempt failed.
custom_keycloak | 2024-03-24 11:16:00,093 WARN [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator] (JPA Startup Thread: keycloak-default) HHH000342: Could not obtain co
nnection to query metadata: org.postgresql.util.PSQLException: The connection attempt failed.


custom_keycloak | 2024-03-24 11:16:00,923 WARN [io.agroal.pool] (agroal-11) Datasource ‘’: The connection attempt failed.
custom_keycloak | 2024-03-24 11:16:01,161 ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler] (main) **ERROR: Failed to start server in (development) mode
custom_keycloak | 2024-03-24 11:16:01,161 ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler] (main) **ERROR: Failed to obtain JDBC connection
custom_keycloak | 2024-03-24 11:16:01,161 ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler] (main) ERROR:The connection attempt failed.
custom_keycloak | 2024-03-24 11:16:01,161 ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler] (main) ERROR: postgres

And here is my docker compose with my sql.init:

version: '3.8'
services:
  postgres_db:
    image: postgres:latest
    ports:
      - "5432:5432"
    container_name: Employee_Db
    restart: always
    environment:
      POSTGRES_DB: employee
      POSTGRES_USER: employee
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    env_file:
      - .env
    networks:
      - pg-network
    volumes:
      - ./db-data/:/var/lib/postgresql/data/
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql

  pgadmin:
    image: dpage/pgadmin4:latest
    restart: always
    container_name: pg-admin
    ports:
      - "5050:80"
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD}
      PGADMIN_CONFIG_SERVER_MODE: "False"
      PGADMIN_CONFIG_MASTER_PASSWORD_REQUIRED: "False"
    volumes:
      - ./servers.json:/pgadmin4/servers.json
    depends_on:
      - postgres_db
    networks:
      - pg-network

  keycloak:
    image: quay.io/keycloak/keycloak:21.1.1
    container_name: custom_keycloak
    restart: unless-stopped
    ports:
      - "8180:8180"
    environment:
      KEYCLOAK_ADMIN: admin
      KEYCLOAK_ADMIN_PASSWORD: admin
      KC_DB: postgres
      KC_DB_SCHEMA: keycloak
      KC_DB_USERNAME: postgres
      KC_DB_PASSWORD: postgres
      KC_DB_URL_PORT: 5432
      KC_DB_URL_HOST: postgres
      KC_DB_URL: jdbc:postgresql://postgres:5432/employee
      KC_HTTP_ENABLED: true
      KC_HOSTNAME_STRICT: false
      KC_HOSTNAME_STRICT_HTTPS: false
      KEYCLOAK_EXTRA_ARGS: --import-realm
    depends_on:
      - postgres_db
    networks:
      - pg-network
    command:
      - start-dev
      - --http-port=8180
      - --import-realm
    volumes:
       - ./keycloak/import_to_keycloak/howtodoinjava-realm-realm.json:/opt/keycloak/data/import/howtodoinjava-realm-realm.json

networks:
  pg-network:

volumes:
  pg-data:

init.sql - keycloak database is not created,

CREATE USER POSTGRES SUPERUSER;
ALTER USER POSTGRES WITH PASSWORD 'postgres';

CREATE USER admin SUPERUSER;
ALTER USER admin WITH PASSWORD 'admin';
CREATE USER keycloak SUPERUSER;
ALTER USER keycloak WITH PASSWORD 'keycloak';
CREATE DATABASE "keycloak" ; 

CREATE TABLE employee(
    id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
    name VARCHAR(64) NOT NULL,
    phone VARCHAR(10) NOT NULL,
    email VARCHAR(64) NOT NULL,
    position VARCHAR(16) NOT NULL,
    bio TEXT NOT NULL
);

INSERT INTO employee (name, phone, email, position, bio)
VALUES('Nico van de Kamp', '0612341234', 'nico@gmail.com', 'Test', 'Dit is wat je van jezelf wilt vertellen');

-- create a table
CREATE TABLE test(
    id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
    name TEXT NOT NULL,
    archived BOOLEAN NOT NULL DEFAULT FALSE
);

-- add test data
INSERT INTO test (name, archived)
VALUES ('test row 1', true),
       ('test row 2', false);