psql无法访问在Docker容器中运行的Postgres

3
我已经成功构建了一个基于Postgres的Docker image,使得PostGIS能够运行

我现在运行它:

docker run -d -t -p 5432:5432 -v ./data:/data --name postgis-osm-pgrouting -e POSTGRES_PASSWORD=postgres pamtrak06/postgis-pgrouting-osm bash

然而,当我尝试通过psql连接到数据库时:
psql -h localhost -p 5432 postgres

我遇到了一个错误:

psql: server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.

我是一个端口转发的初学者,但看起来这是一个与端口有关的问题。
有什么想法吗?

容器应该能够在没有端口转发的情况下启动,所以我怀疑那不是问题所在。你能获取服务器日志文件吗? - OneCricketeer
如果从docker run命令中删除-t标志和bash命令,会发生什么? - sp0gg
2个回答

2

要从容器内访问应用程序,您需要首先“附加”到该容器。

您可以通过运行以下命令来执行此操作:

docker exec -it container_name sh

这个命令的作用是在容器container_name内运行sh命令。它会提示一个shell终端,你可以在其中运行如下的psql命令:
psql -U postgres

在这里,您正在使用用户postgres(psql的默认授权用户)运行psql


-1

试一下这个

docker run -d -t -p 5432:5432 -v $PWD/data:/data --name postgis-osm-pgrouting -e POSTGRES_PASSWORD=postgres pamtrak06/postgis-pgrouting-osm

然后

psql -h localhost -p 5432 postgres

你有:

psql: could not connect to server: Connection refused
Is the server running on host "192.168.99.101" and accepting
TCP/IP connections on port 5432?

因此,应用[配置PostgreSQL以接受TCP/IP连接][https://www.mozmorris.com/2011/11/15/configure-postgresql-to-accept-tcpip-connections.html],但不要在生产环境中使用,仅用于测试目的!

并使用此配置覆盖您的Dockerfile。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接