Search code examples
postgresqldocker

FATAL: role "root" does not exist in docker


I am trying to run KeyLock server locally

I am running this docker-compose file: sudo docker-compose -f file_name.yaml up --build

version: "3.9"
services:
  keycloak-postgres:
    image: library/postgres:${KC_POSTGRES_IMAGE_TAG:-14}
    container_name: ${POSTGRES_CONTAINER_NAME:-postgres}
    restart: on-failure
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres
    healthcheck:
      test: pg_isready -d postgres
      interval: 10s
      timeout: 5s
      retries: 3
      start_period: 5s
    ports:
      - ${KC_POSTGRES_PORT_MAPPING:-5435}:5432
    deploy:
      resources:
        limits:
          memory: 256M

  keycloak:
    image: quay.io/keycloak/keycloak:20.0.2
    container_name: keycloak
    command:
      - start --auto-build --db postgres --hostname-strict-https false --hostname-strict false --proxy edge --http-enabled true --import-realm --spi-user-profile-legacy-user-profile-read-only-attributes *_RES_ACCESS_MODE
    environment:
      KC_DB_URL: jdbc:postgresql://keycloak-postgres:5432/postgres
      KC_DB_USERNAME: postgres
      KC_DB_PASSWORD: postgres
      KC_DB_SCHEMA: public
      KC_FEATURES: preview
      KEYCLOAK_ADMIN: admin
      KEYCLOAK_ADMIN_PASSWORD: admin
    volumes:
      - type: bind
        source: ./realm-export.json
        target: /opt/keycloak/data/import/realm-export.json
        read_only: true
    ports:
      - 8282:8080
    depends_on:
      keycloak-postgres:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://0.0.0.0:8080/realms/master"]
      start_period: 10s
      interval: 30s
      retries: 3
      timeout: 5s

I have an error: (full log https://pastebin.com/gvFXfPAy )

postgres  | 2024-04-11 21:54:28.799 UTC [1] LOG:  database system is ready to accept connections
postgres  | 2024-04-11 21:54:38.880 UTC [42] FATAL:  role "root" does not exist
...
recovery is not enabled. Please enable transaction recovery by setting quarkus.transaction-manager.enable-recovery=true, otherwise data may be lost if the application is terminated abruptly
postgres  | 2024-04-11 21:54:43.032 UTC [43] FATAL:  password authentication failed for user "postgres"
postgres  | 2024-04-11 21:54:43.032 UTC [43] DETAIL:  Role "postgres" does not exist.
postgres  |     Connection matched pg_hba.conf line 100: "host all all all scram-sha-256"
keycloak  | 2024-04-11 21:54:43,035 WARN  [io.agroal.pool] (agroal-11) Datasource '<default>': FATAL: password authentication failed for user "postgres"

I have checked my postgres users:

mypc=# \du
                                     List of roles
  Role name   |                         Attributes                         | Member of 
--------------+------------------------------------------------------------+-----------
 mypc | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 postgres     | Superuser                                                  | {}
 root         | Superuser                                                  | {}

Also I was changing password

ALTER USER postgres WITH PASSWORD 'postgres';

Solution

  • From your log line 48 :FATAL: password authentication failed for user "postgres"

    It means you use the wrong username and password.

    You need to change username and password.

    From

          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: postgres
    
          KC_DB_USERNAME: postgres
          KC_DB_PASSWORD: postgres
    
    

    To

          POSTGRES_PASSWORD: keycloak
          POSTGRES_DB: password
    
          KC_DB_USERNAME: keycloak
          KC_DB_PASSWORD: password
    

    Demo Save as docker-compose.yml

    version: '3.9'
    
    services:
      postgres:
        image: postgres:14.11
        container_name: postgres_db
        volumes:
          - postgres_data:/var/lib/postgresql/data
        environment:
          POSTGRES_DB: keycloak
          POSTGRES_USER: keycloak
          POSTGRES_PASSWORD: password
    
      keycloak_web:
        image: quay.io/keycloak/keycloak:20.0.2
        container_name: keycloak_web
        environment:
          KC_DB: postgres
          KC_DB_URL: jdbc:postgresql://postgres:5432/keycloak
          KC_DB_USERNAME: keycloak
          KC_DB_PASSWORD: password
    
          KC_HOSTNAME: localhost
          KC_HOSTNAME_STRICT: false
          KC_HOSTNAME_STRICT_HTTPS: false
    
          KC_LOG_LEVEL: info
          KC_METRICS_ENABLED: true
          KC_HEALTH_ENABLED: true
          KEYCLOAK_ADMIN: admin
          KEYCLOAK_ADMIN_PASSWORD: admin
        command: start-dev
        depends_on:
          - postgres
        ports:
          - 8280:8080
    
    volumes:
      postgres_data:
    

    Launching it

    docker compose up
    

    Result

    http://localhost:8280
    

    enter image description here