21

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 ?

6
  • You showed us how you configured postgresql, but how did you configure GitHub runner? Commented Dec 2, 2021 at 23:02
  • 1
    I don't use Docker, but I am going to say the issue is:test: [ "CMD-SHELL", "pg_isready" ]. pg_isready without -U is going to use the system user as the database user. Pretty sure in this case the system user where the command is running is root. pg_isready is libpq based so it will recognize suitable env variables which for the user is PGUSER per libpq env not POSTGRES_USER. Commented Dec 2, 2021 at 23:35
  • How can I supply user for health check probe ? Commented Dec 3, 2021 at 6:29
  • I would try either "pg_isready -U pguser" or add PGUSER: pguser to environment section. Like I said I'm not a Docker user, so this is educated guesses at best. Commented Dec 3, 2021 at 16:25
  • @AdrianKlaver I have tried setting up the env PGUSER and add -U pguser to the pg_isready command, it does not work. Commented Mar 8, 2022 at 2:46

3 Answers 3

65

should set POSTGRES_DB and POSTGRES_USER in healthcheck

healthcheck:
    test: [ "CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}" ]
Sign up to request clarification or add additional context in comments.

2 Comments

Not entirely sure why this would be the case (why would you have to specify the credentials in the first place?)... but this is what solved it for me.
I was missing POSTGRES_DB and POSTGRES_USER, thank you
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

3 Comments

why you don't set -U postgres in docker container ?
` initial user as "postgres"`this is postgres users ,not docker default user
I'll have to look into it, I use docker compose to set up my postgres container, and it isn't currently seeming to honor my initial POSTGRES_DB, POSTGRES_USER, POSTGRES_PASSWORD env vars
0

If you are running alpine postgres you may need to change the healthcheck to run as the postgres user and not root, by putting the following in your compose file:

healthcheck:
      test: ["CMD-SHELL", "su -c 'pg_isready -U ${POSTGRES_USER} -h localhost' postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.