我正在开发一个使用 postgres 数据库的应用程序。我使用在容器中运行所有内容docker-compose
。
我的撰写文件如下所示:
version: '3.8'
services:
backend:
... # Backend stuff, runs fine
depends_on:
- db
frontend:
... #Frontend stuff, runs fine
db:
image: postgres
volumes:
- ./db/data:/var/lib/postgresql/data
ports:
- 5432:5432
environment:
- POSTGRES_PASSWORD=*********** #This is a proper password
healthcheck:
test: ["CMD","pg_isready", "-d", "postgres", "-h", "localhost", "-p 5432", "-U postgres"]
interval: 10s
timeout: 30s
retries: 5
start_period: 30s
一切正常并且运行良好,但是,我的数据库容器不断打印出以下错误消息:
FATAL: role " postgres" does not exist
我没有指定任何用户,因此根据我的理解,在构建映像时应创建默认角色“postgres”。
我也看到很多人在他们的系统上运行其他 postgres 数据库时遇到这个问题,但对我来说情况并非如此。
附言。
我的后端是使用 FastAPI 和 SQLAlchemy 以及 psycopg 用 python 编写的。它使用 URL 连接到数据库'postgresql+psycopg://postgres:DBPASSWORD@db:5432'