postgresql 如何进入正在运行的 postgres 容器的 psql?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27673563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get into psql of a running postgres container?
提问by sargas
I created a postgres container using the tutorial on the fig website. I named the container db.
我使用fig 网站上的教程创建了一个 postgres 容器。我将容器命名为db。
The container is running and my app connects to it fine. I tried to run the command fig run db psql
with the dbcontainer running and got the error:
容器正在运行,我的应用程序可以正常连接到它。我尝试在运行db容器的情况下运行该命令fig run db psql
,但出现错误:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
How can I get into the psql
interface in the running dbcontainer?
如何进入psql
正在运行的db容器中的界面?
回答by sargas
figwill create a docker containerwith a different name than the one used in the fig.yml
file.
fig将创建一个docker 容器,其名称与fig.yml
文件中使用的名称不同。
I got it working by finding the container name with docker ps
and looking at the NAMEScolumn.
我通过查找容器名称docker ps
并查看NAMES列来使其工作。
Then running the psql
command in the running container with docker exec -ti NAME_OF_CONTAINER psql -U YOUR_POSTGRES_USERNAME
然后psql
在正在运行的容器中运行命令docker exec -ti NAME_OF_CONTAINER psql -U YOUR_POSTGRES_USERNAME
Note that docker exec
runs the psql
command on a running container, as opposed to docker run
which will start a new container.
请注意,在正在运行的容器上docker exec
运行该psql
命令,而不是docker run
将启动一个新容器。
Update
更新
figis now called docker-compose
fig现在被称为docker-compose
回答by dukebody
You need to run a new container to connect to the one started by fig. This is so because the main container by default starts the service, and if you do fig run db psql
fig will NOT start the service but run the psql client instead. See the Dockerfile.
您需要运行一个新容器来连接到由 fig 启动的容器。这是因为默认情况下主容器启动服务,如果你这样做fig run db psql
fig 将不会启动服务而是运行 psql 客户端。请参阅Dockerfile。
So to connect to the PostgreSQL service you need to run another containerlinked to the one started by fig. See https://registry.hub.docker.com/_/postgres/.
因此,要连接到 PostgreSQL 服务,您需要运行另一个容器,该容器链接到由 fig 启动的容器。请参阅https://registry.hub.docker.com/_/postgres/。
First, since fig changes the names of the containers started, check the NAMES column of the docker ps
container after having done fig up
. Then:
首先,由于 fig 更改了启动的容器的名称,因此docker ps
在完成后检查容器的 NAMES 列fig up
。然后:
docker run -it --link <postgres_container_name>:postgres --rm postgres sh -c 'exec psql -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U postgres'
You can do the docker exec
trick as well as desribed by @sargas too, but the linking way sounds more canonical to me.
您也可以docker exec
使用@sargas 所描述的技巧,但链接方式对我来说听起来更规范。
回答by Justin
Can you post the result of docker ps
? My guess is you need to specify the port the postgres container is exposing. Running docker ps
should give you
你能发布结果docker ps
吗?我的猜测是您需要指定 postgres 容器公开的端口。跑步docker ps
应该给你
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
948b1f6ebc0a my_postgres:latest "/usr/lib/postgresql 6 days ago Up 6 days 0.0.0.0:49155->5432/tcp db
and looking under the PORTS column for your db container you'll see the port the db is actually exposed on. In this case it's 49155, but docker will choose a random port between 49153 and 65535 if not explicitly specified at container start. You need to supply the -p
option to psql to then target that port as such
在 PORTS 列下查看 db 容器,您将看到 db 实际暴露的端口。在这种情况下,它是 49155,但如果在容器启动时未明确指定,docker 将在 49153 和 65535 之间选择一个随机端口。您需要为-p
psql提供选项,然后以此为目标端口
psql -p 49155 ...
回答by Thomas Escolan
My 2P here:
我的 2P 在这里:
docker run --rm --name postgresql -p 5432:5432
-e POSTGRES_USER=admin -e POSTGRES_PASSWORD=admin
-e POSTGRES_DB=demodb
-d postgres:latest
docker exec -it postgresql psql -d demodb -U admin
回答by Tomas Zubiri
Instead of connecting with:
而不是连接:
psql
Use the -h and -p option:
使用 -h 和 -p 选项:
psql -h localhost -p 5432
Why did that work?
为什么这样做?
If we run a local psql without arguments (or with incorrect arguments), psql will try to connect via socket instead of tcp, because this is a tad more efficient.
如果我们不带参数(或不正确的参数)运行本地 psql,psql 将尝试通过套接字而不是 tcp 进行连接,因为这样效率更高。
However, psql's micro optimization doesn't work for our quirky setup, since our container's file system is separate by design, and even if it weren't psql wouldn't know where to look for the socket.
然而,psql 的微优化对我们古怪的设置不起作用,因为我们容器的文件系统在设计上是分开的,即使不是,psql 也不知道在哪里寻找套接字。
The solution is not writing a burly docker exec command, nor is it mounting a volume so that our local psql instance can find the socket in the container, but to move the whole interaction to tcp.
解决的办法不是写一个笨重的docker exec命令,也不是挂载一个volume让我们本地的psql实例可以在容器中找到socket,而是把整个交互移到tcp上。
Sure this is slightly less efficient, but the very reason we are using a container is so that things work even if they are setup in different computers, TCP is what docker containers use to communicate between processes, whether in the same machine or not..
当然这效率稍低,但我们使用容器的真正原因是即使它们设置在不同的计算机上也能正常工作,TCP 是 docker 容器用来在进程之间进行通信的东西,无论是否在同一台机器上。.
To tell psql that we want to connect via TCP, we use the -h option, along with the -p for port.
要告诉 psql 我们想通过 TCP 连接,我们使用 -h 选项,以及 -p 用于端口。
psql -h localhost -p 5432
Now, couldn't psql try to connect via tcp if it can't find a socket?
现在,如果找不到套接字,psql 不能尝试通过 tcp 连接吗?