我有一个用于 DB 的 Docker 镜像(安装了 postgres),我尝试运行它,但我得到以下信息:
db | 2018-11-27 08:29:28.849 UTC [2808] FATAL: no pg_hba.conf entry for host "172.x.x.x", user "postgres", database "postgres", SSL off
我尝试了以下方法:
- 要更改IP并把我当前机器的ip
- 当设置 Postgres 时:
echo -e "host all all 0.0.0.0/0 trust"
我把那个
这些都不起作用。
最佳答案
让我的应用程序容器与我的 postgres 容器通信时,我遇到了同样的问题。我通过三个步骤解决了这个问题:
- 为 docker 网络定义网络 ip 地址范围
# +----------------------+
# | docker-compose.yml |
# +----------------------+
networks:
my_docker_network:
driver: bridge
ipam:
config:
- subnet: 10.5.0.0/16
- 在 postgres pg_hba.conf 文件中将相同的网络 ip 地址范围列入白名单
# +---------------+
# | pg_hba.conf |
# +---------------+
# TYPE DATABASE USER ADDRESS METHOD
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 10.5.0.0/16 trust
我使用 trust
来避免身份验证的麻烦,但您可以指定 md5
、cert
或任何其他有效的身份验证方法。
- 在 postgresql.conf 文件中允许 postgres 监听外部主机
# +-------------------+
# | postgresql.conf |
# +-------------------+
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
关于postgresql - 主机的 Docker postgres 镜像 : no pg_hba. conf 条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53495643/