Docker - 如何在 postgres 容器中运行 psql 命令? [英] Docker - How can run the psql command in the postgres container?
问题描述
我想在 postgres 图像中使用 psql 以便在数据库上运行一些查询.但不幸的是,当我附加到 postgres 容器时,我得到了那个错误 psql 命令未找到...
I would like to use the psql in the postgres image in order to run some queries on the database. But unfortunately when I attach to the postgres container, I got that error the psql command is not found...
对我来说,如何在容器中运行 postgre sql 查询或命令是一个谜.
For me a little bit it is a mystery how I can run postgre sql queries or commands in the container.
如何在 postgres 容器中运行 psql 命令?(我是 Docker 世界的新人)
How run the psql command in the postgres container? (I am a new guy in Docker world)
我使用 Ubuntu 作为主机,我没有在主机上安装 postgres,而是使用 postgres 容器.
I use Ubuntu as a host machine, and I did not install the postgres on the host machine, I use the postgres container instead.
docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------
yiialkalmi_app_1 /bin/bash Exit 0
yiialkalmi_nginx_1 nginx -g daemon off; Up 443/tcp, 0.0.0.0:80->80/tcp
yiialkalmi_php_1 php-fpm Up 9000/tcp
yiialkalmi_postgres_1 /docker-entrypoint.sh postgres Up 5432/tcp
yiialkalmi_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
这里是容器:
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
315567db2dff yiialkalmi_nginx "nginx -g 'daemon off" 18 hours ago Up 3 hours 0.0.0.0:80->80/tcp, 443/tcp yiialkalmi_nginx_1
53577722df71 yiialkalmi_php "php-fpm" 18 hours ago Up 3 hours 9000/tcp yiialkalmi_php_1
40e39bd0329a postgres:latest "/docker-entrypoint.s" 18 hours ago Up 3 hours 5432/tcp yiialkalmi_postgres_1
5cc47477b72d redis:latest "docker-entrypoint.sh" 19 hours ago Up 3 hours 6379/tcp yiialkalmi_redis_1
这是我的 docker-compose.yml:
And this is my docker-compose.yml:
app:
image: ubuntu:16.04
volumes:
- .:/var/www/html
nginx:
build: ./docker/nginx/
ports:
- 80:80
links:
- php
volumes_from:
- app
volumes:
- ./docker/nginx/conf.d:/etc/nginx/conf.d
php:
build: ./docker/php/
expose:
- 9000
links:
- postgres
- redis
volumes_from:
- app
postgres:
image: postgres:latest
volumes:
- /var/lib/postgres
environment:
POSTGRES_DB: project
POSTGRES_USER: project
POSTGRES_PASSWORD: project
redis:
image: redis:latest
expose:
- 6379
推荐答案
docker exec -it yiialkalmi_postgres_1 psql -U project -W project
一些解释
docker exec -it
对正在运行的容器运行命令的命令.it
标志打开一个交互式 tty.基本上它会导致附加到终端.如果你想打开 bash 终端,你可以这样做
docker exec -it
The command to run a command to a running container. Theit
flags open an interactive tty. Basically it will cause to attach to the terminal. If you wanted to open the bash terminal you can do this
docker exec -it yiialkalmi_postgres_1 bash
yiialkalmi_postgres_1
容器名称(您可以使用容器 ID,在您的情况下为40e39bd0329a
)
yiialkalmi_postgres_1
The container name (you could use the container id instead, which in your case would be40e39bd0329a
)
psql -U project -W project
执行到正在运行的容器的命令
psql -U project -W project
The command to execute to the running container
U
用户
W
告诉 psql 在连接时需要提示用户输入密码.这个参数是可选的.如果没有这个参数,会有一个额外的连接尝试,通常会发现需要密码,参见PostgreSQL 文档.
W
Tell psql that the user needs to be prompted for the password at connection time. This parameter is optional. Without this parameter, there is an extra connection attempt which will usually find out that a password is needed, see the PostgreSQL docs.
project
要连接的数据库.当 -d
参数是第一个非选项参数时,不需要将其标记为 dbname,请参阅文档:-d
"相当于将 dbname 指定为命令行上的第一个非选项参数."
project
the database you want to connect to. There is no need for the -d
parameter to mark it as the dbname when it is the first non-option argument, see the docs: -d
"is equivalent to specifying dbname as the first non-option argument on the command line."
这些是您在此处指定的
environment:
POSTGRES_DB: project
POSTGRES_USER: project
POSTGRES_PASSWORD: project
这篇关于Docker - 如何在 postgres 容器中运行 psql 命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!