Docker Compose部署Commento无法连接Postgres问题求助
问题修复方案
错误日志
2023/02/01 08:34:35 [INFO] version.go:13 versionPrint(): starting Commento 2023/02/01 08:34:35 [INFO] database_connect.go:20 dbConnect(): opening connection to postgres: postgres://postgres:redacted@db:5432/commento?sslmode=disable 2023/02/01 08:34:35 [ERROR] database_connect.go:31 dbConnect(): cannot talk to postgres, retrying in 10 seconds (4 attempts left): dial tcp 172.18.0.2:5432: connect: connection refused 2023/02/01 08:34:45 [INFO] database_connect.go:20 dbConnect(): opening connection to postgres: postgres://postgres:redacted@db:5432/commento?sslmode=disable 2023/02/01 08:34:45 [ERROR] database_connect.go:31 dbConnect(): cannot talk to postgres, retrying in 10 seconds (3 attempts left): pq: unknown authentication response: 10 2023/02/01 08:34:55 [INFO] database_connect.go:20 dbConnect(): opening connection to postgres: postgres://postgres:redacted@db:5432/commento?sslmode=disable 2023/02/01 08:34:55 [ERROR] database_connect.go:31 dbConnect(): cannot talk to postgres, retrying in 10 seconds (2 attempts left): pq: unknown authentication response: 10 2023/02/01 08:35:05 [INFO] database_connect.go:20 dbConnect(): opening connection to postgres: postgres://postgres:redacted@db:5432/commento?sslmode=disable 2023/02/01 08:35:06 [ERROR] database_connect.go:31 dbConnect(): cannot talk to postgres, retrying in 10 seconds (1 attempts left): pq: unknown authentication response: 10 2023/02/01 08:35:16 [INFO] database_connect.go:20 dbConnect(): opening connection to postgres: postgres://postgres:redacted@db:5432/commento?sslmode=disable 2023/02/01 08:35:16 [ERROR] database_connect.go:31 dbConnect(): cannot talk to postgres, retrying in 10 seconds (0 attempts left): pq: unknown authentication response: 10 2023/02/01 08:35:26 [INFO] database_connect.go:20 dbConnect(): opening connection to postgres: postgres://postgres:redacted@db:5432/commento?sslmode=disable 2023/02/01 08:35:26 [ERROR] database_connect.go:35 dbConnect(): cannot talk to postgres, last attempt failed: pq: unknown authentication response: 10 fatal error: pq: unknown authentication response: 10
复现步骤
- 在全新Ubuntu 22.04LTS服务器上安装docker和docker-compose。
- 创建服务目录,使用以下
docker-compose.yml配置:
version: '3' services: server: image: registry.gitlab.com/commento/commento:SET_VERSION ports: - 8080:8080 environment: COMMENTO_ORIGIN: http://commento.example.com:8080 COMMENTO_PORT: 8080 COMMENTO_POSTGRES: postgres://postgres:postgres@db:5432/commento?sslmode=disable depends_on: - db db: image: postgres environment: POSTGRES_DB: commento POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres volumes: - postgres_data_volume:/var/lib/postgresql/data volumes: postgres_data_volume:
- 执行
docker-compose up -d后查看服务日志,commento_server_1服务出现上述错误。
修复方法
原因说明
unknown authentication response: 10错误是因为PostgreSQL 14+默认使用scram-sha-256认证方式,而旧版本Commento使用的pq数据库驱动不支持该认证,导致连接失败。
方案1:指定PostgreSQL兼容版本
修改docker-compose.yml中的postgres镜像版本,使用PostgreSQL 13或更低版本(比如13-alpine),这些版本默认使用旧的md5认证:
db: image: postgres:13-alpine # 替换原postgres镜像 environment: POSTGRES_DB: commento POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres volumes: - postgres_data_volume:/var/lib/postgresql/data
修改后执行以下命令重启服务:
docker-compose down -v # 删除旧数据卷,避免残留配置 docker-compose up -d
方案2:强制PostgreSQL使用md5认证
如果不想降低PostgreSQL版本,可以在db服务中添加环境变量POSTGRES_INITDB_ARGS,强制初始化时使用md5认证:
db: image: postgres environment: POSTGRES_DB: commento POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres POSTGRES_INITDB_ARGS: "--auth-host=md5" # 添加该行 volumes: - postgres_data_volume:/var/lib/postgresql/data
同样需要删除旧数据卷后重启:
docker-compose down -v docker-compose up -d
额外注意事项
- 确保
COMMENTO_POSTGRES环境变量中的用户名、密码、数据库名和postgres服务的配置完全一致。 depends_on仅保证服务启动顺序,不保证postgres完全就绪,Commento本身有重试机制,所以不用额外添加健康检查(如果重试后仍失败,就是认证问题)。
内容的提问来源于stack exchange,提问作者dummytokken
相关产品推荐
相关产品推荐

