【发布时间】:2016-08-11 05:06:31
【问题描述】:
我正在使用 postgres:9.5.3 docker 映像。我正在启动容器,然后尝试从远程主机连接到 psql 数据库,但每次都失败并出现错误:
psql: could not connect to server: Connection refused
Is the server running on host "172.18.0.2" and accepting
TCP/IP connections on port 5432?
在我的 docker-compose 文件中,我正在挂载 pg_hba.conf。这是我的 docker-compose 文件:
services:
db:
networks:
- test
image: postgres:9.5.3
expose:
- 5432
volumes:
- ./pgdata/:/var/lib/postgresql/data
- ./pg_hba.conf/:/var/lib/postgresql/data/pg_hba.conf
我已根据here 的说明修改了我的 pg_hba.conf 文件以接受来自所有主机的远程连接。我的pg_hba.conf如下:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 0.0.0.0/0 trust
# IPv6 local connections:
host all all ::0/0 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres trust
#host replication postgres 127.0.0.1/32 trust
#host replication postgres ::1/128 trust
host all all 0.0.0.0/0 md5
我的 postgresql.conf 也有以下行:listen_addresses = '*'
当我尝试从运行容器的主机连接到数据库时,它连接成功。但是当我尝试使用命令psql -h 172.18.0.2 -U postgres -d postgres -p 5432 从任何远程计算机连接时,它给了我连接错误,这意味着远程连接不起作用。通过所有这些设置,我希望它能够连接。我在这里错过了什么?
【问题讨论】:
-
"172.18.0.2" 172.0.0.0/8 是桥接/路由/防火墙吗? IIRC 172. 网络有一些特别之处。
标签: postgresql docker psql