postgres 与 docker compose 给出 FATAL: role "root" does not exit 错误

2024-05-01

我正在尝试在具有 docker 桌面的本地 Windows 计算机上使用 postgres 创建一个简单的演示。
这是我的 yaml docker compose 文件,名为img.yaml:

version: '3.6'

services:

    postgres-demo:
      image: postgres:11.5-alpine
      container_name: postgres-demo
      environment:
        - POSTGRES_USER=postgres
        - POSTGRES_PASSWORD=Welcome
        - POSTGRES_DB=conference_app
      healthcheck:
        test: ["CMD-SHELL", "pg_isready -U postgres"]
        interval: 10s
        timeout: 5s
        retries: 5
      ports:
        - 5432:5432
      volumes:
        - .:/var/lib/my_data
      restart: always

我使用以下命令运行它:docker-compose -f img.yaml up并得到以下输出:

Starting postgres-demo ... done
Attaching to postgres-demo
postgres-demo    | 2020-02-12 17:07:46.487 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres-demo    | 2020-02-12 17:07:46.487 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres-demo    | 2020-02-12 17:07:46.508 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres-demo    | 2020-02-12 17:07:46.543 UTC [18] LOG:  database system was shut down at 2020-02-12 17:07:10 UTC
postgres-demo    | 2020-02-12 17:07:46.556 UTC [1] LOG:  database system is ready to accept connections

然后,使用以下命令在容器中打开 bash:docker exec -it d47056217a97 bash
我想查看容器中的数据库,因此我在 bash 中运行命令:psql \dt
并得到错误:psql: FATAL: role "root" does not exist.
尝试使用以下命令创建数据库:psql> create database conference_app;给出错误:psql: FATAL: role "conference_app" does not exist.
我很困惑。我究竟做错了什么?我的 yaml 缺少什么吗?


如果您不指定PGUSER环境变量,那么psql将假设您想要使用当前操作系统用户作为数据库用户名。在这种情况下,您正在使用root作为您的操作系统用户,您将尝试以以下身份登录root,但该用户在数据库中不存在。

您需要致电psql -U postgres, or su - Postgres first

另请参阅PostgreSQL 文档 https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-USER

更新:有人建议更改PGUSER to POSTGRES_USER——这其实是错误的。 Postgres 寻找PGUSER在环境中,但如果您使用 Docker,您将通过使用告诉 Docker 正确的用户POSTGRES_USER,它被分配给PGUSER——参见入口点源代码 https://github.com/docker-library/postgres/blob/master/docker-entrypoint.sh#L266

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

postgres 与 docker compose 给出 FATAL: role "root" does not exit 错误 的相关文章

随机推荐