Hasura Docker快速启动遇pg_hba.conf连接错误求助
解决Hasura graphql-engine容器连接PostgreSQL报错问题
临时快速修复(适配当前运行容器)
- 进入运行中的PostgreSQL容器:
docker exec -it <postgres容器名或ID> bash - 编辑
/var/lib/postgresql/data/pg_hba.conf文件,新增一行规则:
这条规则覆盖Docker默认网桥的IP段,确保Hasura容器的IP能匹配通过。host postgres postgres 172.23.0.0/16 trust - 容器内重载PostgreSQL配置生效:
pg_ctl reload -D /var/lib/postgresql/data
持久化修复(修改docker-compose配置)
通过自定义配置让PostgreSQL启动时自动加载允许连接的规则:
- 在docker-compose.yaml所在目录新建
pg_hba.conf文件,写入以下内容:local all postgres trust local all all trust host all all 127.0.0.1/32 trust host all all ::1/128 trust # 允许Docker常见网桥IP段无加密连接 host all all 172.17.0.0/16 trust host all all 172.23.0.0/16 trust - 修改docker-compose.yaml中PostgreSQL的volumes配置,挂载自定义的pg_hba文件:
services: postgres: volumes: - ./pg_hba.conf:/var/lib/postgresql/data/pg_hba.conf - hasura-postgres-data:/var/lib/postgresql/data - 重启所有容器:
docker-compose down && docker-compose up -d
补充说明
Windows/WSL环境下Docker的网桥IP段和Mac存在差异,默认PostgreSQL配置未覆盖该段,导致无加密连接被拒绝。trust认证仅适合本地开发场景,生产环境请替换为md5等安全认证方式。
内容的提问来源于stack exchange,提问作者danielliem49
相关产品推荐
相关产品推荐

