【问题标题】:unable to launch psql command while running a postgres contenair运行 postgres 容器时无法启动 psql 命令
【发布时间】:2022-11-10 22:57:42
【问题描述】:

我尝试使用 localhost 推荐的命令来学习玩 docker。 确切的命令是:

docker run -it --rm postgres psql

我得到的错误信息是:

psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
    Is the server running locally and accepting connections on that socket?

实际上文件 .s.PGSQL.5432 在容器中不存在,但它存在 在主机上。 那么,我的推理/命令有什么问题?

【问题讨论】:

  • 当您像这样运行 psql 时,它需要一个可以连接到的正在运行的 postgres 实例。你有吗?

标签: postgresql docker psql


【解决方案1】:

从概念上讲,您应该将容器视为独立的机器。与宿主分离,彼此分离。

当你在没有任何参数的情况下运行 psql 时,就像你在这里做的那样,它会在本地机器上的 5432 端口上查找运行的 postgres 数据库。但是由于 psql 在容器中运行,它会在容器内查找数据库。而且没有。这就是错误消息试图告诉您的内容。

为了让它工作,你需要在 psql 命令上指定-h 参数来告诉它数据库的位置。要获取宿主机的地址,可以在 docker run 命令中添加--add-host=host.docker.internal:host-gateway。习惯上称主机host.docker.internal

所以你最终得到了命令

docker run -it --rm --add-host=host.docker.internal:host-gateway postgres psql -h host.docker.internal

然后应该让您连接到主机上的 postgres 数据库。

【讨论】:

    猜你喜欢
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    相关资源
    最近更新 更多