首页 > 解决方案 > 带有 docker compose 的 postgres 给出了 FATAL: role "root" does not exist 错误

问题描述

我正在尝试在带有 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 缺少什么吗?

标签: postgresqldocker-compose

解决方案


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

您需要拨打psql -U postgressu - Postgres先拨打

另请参阅postgresql 文档


推荐阅读