postgresql Docker - 如何在 postgres 容器中运行 psql 命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37099564/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:14:16  来源:igfitidea点击:

Docker - How can run the psql command in the postgres container?

postgresqldockerdocker-compose

提问by Dabagab

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...

我想在 postgres 图像中使用 psql 以便在数据库上运行一些查询。但不幸的是,当我附加到 postgres 容器时,我得到了那个错误 psql 命令未找到...

For me a little bit it is a mystery how I can run postgre sql queries or commands in the container.

对我来说,如何在容器中运行 postgre sql 查询或命令是一个谜。

How run the psql command in the postgres container? (I am a new guy in Docker world)

如何在 postgres 容器中运行 psql 命令?(我是 Docker 世界的新人)

I use Ubuntu as a host machine, and I did not install the postgres on the host machine, I use the postgres container instead.

我使用 Ubuntu 作为主机,我没有在主机上安装 postgres,而是使用 postgres 容器。

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     

Here the containers:

这里的容器:

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

And this is my docker-compose.yml:

这是我的 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

回答by Alkis Kalogeris

docker exec -it yiialkalmi_postgres_1 psql -U project -W project project

Some explanation

一些解释

  • docker exec -itThe command to run a command to a running container. The itflags 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对正在运行的容器运行命令的命令。这些it标志会打开一个交互式 tty。基本上它会导致附加到终端。如果你想打开 bash 终端,你可以这样做

docker exec -it yiialkalmi_postgres_1 bash

docker exec -it yiialkalmi_postgres_1 bash

  • yiialkalmi_postgres_1The container name (you could use the container id instead, which in your case would be 40e39bd0329a)
  • psql -U project -W projectThe command to execute to the running container

  • Uuser

  • Wpassword
  • projectthe database you want to connect to.
  • yiialkalmi_postgres_1容器名称(您可以改用容器 ID,在您的情况下为40e39bd0329a
  • psql -U project -W project执行到正在运行的容器的命令

  • U用户

  • W密码
  • project要连接的数据库。

These are specified by you here

这些是您在此处指定的

environment:
    POSTGRES_DB: project
    POSTGRES_USER: project
    POSTGRES_PASSWORD: project

回答by joselo

If you want to restore the database in a containder you can do this

如果你想在容器中恢复数据库,你可以这样做

docker exec -i app_db_1 psql -U postgres < app_development.back

Dont' forget to add -i

不要忘记添加 -i

:)

:)

回答by thierno

You can enter inside the postgres container using docker-compose by typing the following

您可以使用 docker-compose 通过键入以下内容进入 postgres 容器内部

docker-compose exec postgres bash

docker-compose exec postgres bash

knowing that postgresis the name of the service. Replace it with the name of the Postgresql servicein you docker-compose file.

知道postgres是服务的名称。将其替换为docker-compose 文件中Postgresql 服务的名称。

if you have many docker-compose files, you have to add the specific docker-compose.yml file you want to execute the command with. Use the following commnand instead.

如果您有很多 docker-compose 文件,则必须添加要用于执行命令的特定 docker-compose.yml 文件。请改用以下命令。

docker-compose -f < specific docker-compose.yml> exec postgres bash

docker-compose -f < specific docker-compose.yml> exec postgres bash

For example if you want to run the command with a docker-compose file called local.yml, here the command will be

例如,如果您想使用名为local.yml 的 docker-compose 文件运行命令,这里的命令将是

docker-compose -f local.yml exec postgres bash

docker-compose -f local.yml exec postgres bash

Then, use psql command and specify the database name with the -d flag and the username with the -U flag

然后,使用 psql 命令并使用 -d 标志指定数据库名称,并使用 -U 标志指定用户名

psql -U <database username you want to connect with> -d <database name>

psql -U <database username you want to connect with> -d <database name>

Baammm!!!!! you are in.

巴姆!!!!!你在。

回答by Vojtech Vitek

If you have running "postgres" container:

如果您正在运行“postgres”容器:

docker run -it --rm --link postgres:postgres postgres:9.6 sh -c "exec psql -h $POSTGRES_PORT_5432_TCP_ADDR -p $POSTGRES_PORT_5432_TCP_PORT -U postgres"

回答by vijay

RUN /etc/init.d/postgresql start &&\
    psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
    createdb -O docker docker &&\