You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker Compose部署Postgres容器报错:role " postgres"不存在

问题解决:PostgreSQL容器报FATAL: role " postgres" does not exist

核心问题

你的docker-compose.yml中数据库服务的healthcheck参数格式错误:

test: ["CMD","pg_isready", "-d", "postgres", "-h", "localhost", "-p 5432", "-U postgres"]

这里的"-p 5432"和"-U postgres"把参数标志与对应值合并成了单个字符串,导致pg_isready将带前置空格的" postgres"识别为用户名,自然找不到默认存在的postgres角色。

修正方案

将healthcheck的参数拆分为独立数组元素,每个参数标志(如-U、-p)和对应的值分开编写:

services:
  db:
    # ... 其他配置保持不变
    healthcheck:
      test: ["CMD","pg_isready", "-d", "postgres", "-h", "localhost", "-p", "5432", "-U", "postgres"]
      interval: 10s
      timeout: 30s
      retries: 5
      start_period: 30s

后续操作

  1. 停止并删除当前数据库容器(数据卷保留的话,原有数据不会丢失):
docker-compose stop db
docker-compose rm db
  1. 重新启动数据库服务:
docker-compose up -d db

额外说明

你的后端数据库连接URL格式正确,因此应用能正常访问数据库,只有healthcheck的错误命令在持续输出报错。修正参数格式后,healthcheck即可正确识别postgres用户,报错会停止。

内容的提问来源于stack exchange,提问作者Vetle Hofsøy-Woie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 21:39:53