gpt4 book ai didi

postgresql - Postgres Docker - 无法从远程服务器连接

转载 作者:行者123 更新时间:2023-11-29 14:06:36 24 4
gpt4 key购买 nike

我正在使用 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

我已经根据说明修改了 pg_hba.conf 文件以接受来自所有主机的远程连接 here .我的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 ports without publishing them to the host machine - they’ll only be accessible to linked services. Only the internal port can be specified.

关于postgresql - Postgres Docker - 无法从远程服务器连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38881282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com