postgres docker:角色“root”不存在

roy*_*roy 16 postgresql docker docker-compose

我正在 Github-Action 中运行 docker-compose。docker-compose.yml有以下服务定义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"
Run Code Online (Sandbox Code Playgroud)

但是当容器在农奴托管的 Github-Action 运行器上启动时,我看到以下内容

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
Run Code Online (Sandbox Code Playgroud)

这里缺少什么?

fen*_* ce 44

应在健康检查中设置 POSTGRES_DB 和 POSTGRES_USER

healthcheck:
    test: [ "CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}" ]
Run Code Online (Sandbox Code Playgroud)


Del*_*Png 1

当尝试在 postgresql docker 容器上运行 pg_restore 时,我遇到了类似的错误,我已经在 docker-compose 上设置了 postgresql 并尝试将数据库(从我以前的 postgresql)恢复到它。最初的用户名为“postgres”。我会得到一些错误:

  1. pg_restore:错误:连接到“localhost”(127.0.0.1)的服务器,端口 5432 失败:致命:角色“root”不存在

  2. createuser:错误:连接到套接字“/var/run/postgresql/.s.PGSQL.5432”上的服务器失败:致命:角色“root”不存在

在研究中,我发现我的 pg_restore 命令需要指定用户才能工作(尝试指定角色,但失败了)

这是有效的 pg_restore 命令。也许您可以做类似的事情并找到一种方法来指定您的用户?

#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
Run Code Online (Sandbox Code Playgroud)