PostgreSQL Docker: 角色 "root" 不存在。

11
我正在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"

但是当容器在托管于Serf的Github Actions Runner上启动时,我看到以下信息:

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

这里缺少什么?


你向我们展示了如何配置PostgreSQL,但是你是如何配置GitHub runner的呢? - jjanes
1
我不使用Docker,但是我要说问题是:test: [ "CMD-SHELL", "pg_isready" ]。没有使用 -Upg_isready 将使用系统用户作为数据库用户。在这种情况下,执行该命令的系统用户很可能是 rootpg_isready 基于 libpq,因此它将识别适当的环境变量,对于用户来说是 PGUSER,而不是 POSTGRES_USER,详见 libpq env - Adrian Klaver
我该如何为健康检查探针提供用户? - roy
我会尝试使用 "pg_isready -U pguser" 或者在 environment 部分添加 PGUSER: pguser。就像我说的,我不是 Docker 用户,所以这只是最好的猜测。 - Adrian Klaver
@AdrianKlaver 我已经尝试设置环境变量 PGUSER 并在 pg_isready 命令中添加 -U pguser,但是它没有起作用。 - Hantsy
2个回答

32

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

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

不太确定为什么会出现这种情况(为什么你首先需要指定凭据?)...但这就是解决我的方法。 - stungkit

1

我在运行postgresql docker容器上的pg_restore时遇到了类似的错误,我已经在docker-compose上设置好了postgresql,并尝试将一个数据库(从我的先前的postgresql)恢复到其中。初始用户只是“postgres”。一些我得到的错误:

  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

通过研究,我发现我的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

为什么你不在Docker容器中设置-U postgres? - fcce
将初始用户设置为“postgres”,这是Postgres用户,不是Docker默认用户。 - fcce
我需要调查一下,我使用docker compose来设置我的postgres容器,但它似乎目前没有遵循我的初始POSTGRES_DB、POSTGRES_USER和POSTGRES_PASSWORD环境变量。 - DeltaPng

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接