[英]Postgres Docker - unable to connect from remote server
我正在使用postgres:9.5.3 docker image。 我正在啟動容器,然后嘗試從遠程主機連接到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
我已經根據這里的說明修改了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
從任何遠程計算機進行連接時,它給了我連接錯誤,這意味着遠程連接不起作用。 通過所有這些設置,我希望它能夠連接。 我在這里想念什么?
看來您實際上並沒有發布公開的端口。
代替:
expose:
- 5432
采用:
ports:
- "5432:5432"
expose
文檔:
公開端口而不將其發布到主機上-只有鏈接的服務才能訪問它們。 只能指定內部端口。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.