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

标签 postgresql ubuntu docker port psql

我已经成功构建了一个 postgres基于 Docker image that enables 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.

我是端口转发的初学者,但对我来说这似乎是一个与端口相关的问题。

有什么想法吗?

最佳答案

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

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

docker exec -it container_name sh

此命令的作用是在容器 container_name 中运行命令 sh

它将提示一个 shell 终端,您现在可以在其中运行 psql 命令,如下所示:

psql -U postgres

在这里你用用户 postgres(psql 的默认授权用户)运行 psql

关于postgresql - psql 无法访问在 Docker 容器中运行的 Postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50910022/

相关文章:

postgresql - 我如何让 Postgresql 反射(reflect)对文本搜索配置所做的更改?

PostgreSQL 10 SELECT WHERE 语句

mysql - 禁止访问数据库

docker - 使用 2 个不同版本和端口号运行 Elasticsearch docker 时出现问题

wordpress - Varnish 和 wordpress 之间的 Docker 链接不起作用

postgresql - 使用动态键在 JSON 字段上建立索引

postgresql - 查找 X 天前创建的记录

c++ - 如何包含 draco_dec 库?

python - Cloud Vision API 客户端抛出操作系统错误 "too many open files"

docker - 如何在 docker windows 上使用 --max-concurrent-uploads 标志?