postgresql - 无法远程连接到 Postgres Docker 容器

标签 postgresql docker centos

我有一个使用官方 Postgres docker 镜像在 centos 机器上运行的 docker 容器,它无法远程连接。 sudo docker ps | grep postgres

492192ff9170 postgres "/docker-entrypoint.s" 2 years ago Up 18 hours 0.0.0.0:5432->5432/tcp postgres



当我尝试 psql -h <server domain name> -p 5432 -U postgres在另一台机器上,它刚刚打印出来

psql: could not connect to server: Operation timed out
Is the server running on host [domain name] ([ip address]) and accepting TCP/IP connections on port 5432?



我做了sudo iptables -S | grep 5432在远程服务器上并打印出来

-A DOCKER -d 172.17.0.2/32 ! -i docker0 -o docker0 -p tcp -m tcp --dport 5432 -j ACCEPT



所以它看起来不像 port 5432被阻止

容器内/var/lib/postgresql/data/ ,host all all 0.0.0.0/0 md5已添加到 pg_hba.conf , 尽管postgresql.conflisten_addresses = '*'指定的。

尽管如此,它仍然无法远程访问,尽管我可以使用 psql -h localhost -p 5432 -U postgres 成功连接到本地实例

任何人都可以对这个问题有所了解吗?非常感激。

最佳答案

我遇到了类似的问题,我无法远程连接到 docker 容器中的 postgres db。我敲了两天的头,直到我找到了解决方案。
我通过以下方式配置了我的端口:-p 5438:5432 .换句话说,我试图在我的主机上公开端口 5438。
事实证明,我的主机是安装了 Ubuntu 的 Azure VM。我终于记得在 Azure 门户的“网络”选项卡下,有一个“入站端口规则”列表。
好吧,我需要打开端口 5438。我这样做了,然后 BANG 它开始工作了。 docker config 根本不是问题。
Azure Inbound Port rules

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

相关文章:

python - Django 1.4syncdb + psycopg 因时区错误而失败

docker - 将文件从 Windows 共享到 Boot2docker VM 的最佳方式是什么?

SQL SUM 表达式和锁

ruby-on-rails - 将表列添加到 Group by 子句 - Ruby on Rails - Postgresql

docker-compose 创建目录而不是文件

php - php artisan migrate 命令放在哪里

r - 在 redHat CentOs 7 上安装 R。几个错误和依赖项我无法克服

centos - 为什么 awx 看不到 pip 模块?

mongodb - 在没有YUM的centos上安装MongoDB

postgresql - 使用 lag 函数 postgres 的日期差异