postgresql - 带有 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 缺少什么吗?
解决方案
如果您不指定PGUSER
环境变量,那么psql
将假定您想使用当前操作系统用户作为您的数据库用户名。在这种情况下,您使用root
的是您的操作系统用户,您将尝试以 身份登录root
,但该用户在数据库中不存在。
您需要拨打psql -U postgres
或su - Postgres
先拨打
另请参阅postgresql 文档
推荐阅读
- hdfs - oozie 错误:不允许访问本地文件系统
- asp.net - 可访问 HttpContext 的 ASP.NET 任务调度
- strpos - 在 Strpos 中排除多个 url - 为什么它不起作用?
- java - 使用 Spring Boot 从命令行接收输入
- go - 根据另一个切片中元素的顺序对切片进行排序
- python - 从 python 中的 pandas.Series 中删除特殊字符?
- javascript - 如何使用 Json 对象填充动态添加的输入字段
- android - 如何在 firebase 数据库中找到特定值并获取有关该值的所有信息?将驱动程序与客户问题匹配?
- java - Spring Boot、MySQL、Tomcat 无法在 Eclipse 中创建池的初始连接
- shell - 比较一个文件的两列和另一个文件的两列