'postgres docker : role "root" does not exist

I am running docker-compose in Github-Action. docker-compose.yml has following service definition for postgres

  postgres:
    container_name: postgres
    image: postgres:12
    restart: always
    volumes:
      - ./test/data/init.sql:/docker-entrypoint-initdb.d/init.sql
    environment:
      POSTGRES_DB: "pgdb"
      POSTGRES_USER: "pguser"
      POSTGRES_PASSWORD: "fr2Yitl4BgX"
    ports:
      - "${POSTGRES_PORT:-5432}:5432"
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready" ]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - "local-api"

But when container start on serf-hosted Github-Action runner, I see following

postgres    | 2021-12-02 19:48:33.537 UTC [414] FATAL:  role "root" does not exist
postgres    | 2021-12-02 19:48:43.984 UTC [424] FATAL:  role "root" does not exist
postgres    | 2021-12-02 19:48:54.265 UTC [433] FATAL:  role "root" does not exist
postgres    | 2021-12-02 19:49:04.410 UTC [443] FATAL:  role "root" does not exist

What is missing here ?



Solution 1:[1]

should set POSTGRES_DB and POSTGRES_USER in healthcheck

healthcheck:
    test: [ "CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}" ]

Solution 2:[2]

I'm getting a similar error when trying to run a pg_restore on the postgresql docker container, I had set up postgresql on docker-compose and was trying to restore a db (from my prev postgresql) to it. Just had the initial user as "postgres". Some errors I'd get:

  1. pg_restore: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: role "root" does not exist

  2. createuser: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "root" does not exist

On research I found my pg_restore command needed to specify the User for it to work (tried specifying role, that failed)

Here's the working pg_restore command. Perhaps you can do similarly and find a way to specify user in yours?

#on docker container of psql, command that failed
pg_restore --verbose --clean --no-acl --no-owner -h localhost --role=postgres -d thedbname /home/psql_backups/psql_myapp_220423.dumpbackup

#command that worked
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U postgres -d thedbname /home/psql_backups/psql_myapp_220423.dumpbackup

# lastly (for those doing a pg_restore), from psql commandline, if the restore seems to have worked but don't see data, you may be on the wrong db
\c
\c theDbToSwitchTo

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 feng ce
Solution 2 DeltaPng