无法从主机操作系统使用psql连接到PostgreSQL Docker容器。

huangapple go评论64阅读模式
英文:

cannot connect to postgres docker container from host os with psql

问题

问题

  • 我无法从主机连接到 Docker 容器内的 PostgreSQL 数据库。我正在使用 Node.js 制作一个简单的 CRUD 应用程序,所以从容器内部管理数据库并不适用。

我尝试过的解决方案

  • 我从容器中暴露了端口
  • 在主机操作系统上以 postgres 本地用户身份运行 psql
  • 重定向到不同的本地端口 5432

我正在运行 PostgreSQL 容器的命令是:

docker run --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=secret -e POSTGRES_USER=postgres -d postgres

当运行以下命令时:

psql -h (localhost 或 127.0.0.1) -p 5432 -U postgres

我得到以下错误信息:

"psql: error: connection to server at "127.0.0.1", port 5432 failed: server closed the connection unexpectedly
This probably means the server terminated abnormally before or while processing the request."

附加信息:

docker container ps -a -> f596d7350e9c   postgres   "docker-entrypoint.s…"   6 seconds ago   Up 5 seconds   0.0.0.0:5432->5432/tcp, :::5432->5432/tcp   postgres

ss -lntp -> LISTEN       0            32                 192.168.100.1:53                     0.0.0.0:*
LISTEN       0            4096                     0.0.0.0:5432                   0.0.0.0:*
LISTEN       0            4096                        [::]:5432                      [::]:*

我知道以前有很多类似的问题,但似乎不能解决我的问题。
例如:
https://stackoverflow.com/questions/37694987/connecting-to-postgresql-in-a-docker-container-from-outside

我已经尝试了两天来解决这个问题,但我卡在这一点上。

英文:

Problem

  • Im not able to connect to postgres db inside docker container from host. Im making simple crud app with node.js so managing db from inside of container doesn't come into play.

Solutions I've tried

  • Im exposing port from container
  • running psql as postgres local user on host os
  • redirecting 5432 to different local port

Im running postgres container with:
docker run --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=secret -e POSTGRES_USER=postgres -d postgres

when running
psql -h (localhost or 127.0.0.1) -p 5432 -U postgres
I get
"psql: error: connection to server at "127.0.0.1", port 5432 failed: server closed the connection unexpectedl
This probably means the server terminated abnormally
before or while processing the request."

additional info:
docker container ps -a -> f596d7350e9c postgres "docker-entrypoint.s…" 6 seconds ago Up 5 seconds 0.0.0.0:5432->5432/tcp, :::5432->
5432/tcp postgres

ss -lntp -> LISTEN 0 32 192.168.100.1:53 0.0.0.0:*
LISTEN 0 4096 0.0.0.0:5432 0.0.0.0:*
LISTEN 0 4096 [::]:5432 [::]:*

I know there are bunch of similar questions asked prior to similar problem but they don't seem to solve problem in my case.
ex:
https://stackoverflow.com/questions/37694987/connecting-to-postgresql-in-a-docker-container-from-outside

I've been trying to fix this issue for past 2 days im stuck at this point.

答案1

得分: -1

我曾经在几年前遇到过完全相同的问题。

修复方法是调整你的pg_hba.conf文件以允许你的Docker IP地址。

参见:https://stackoverflow.com/questions/55582527/docker-rundeck-local-postgres

英文:

I've ran into this exact same issue some years ago.

The fix is to adjust your pg_hba.conf to allow your docker ip address in.

See: https://stackoverflow.com/questions/55582527/docker-rundeck-local-postgres

huangapple
  • 本文由 发表于 2023年7月3日 22:58:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定