postgresql docker-compose:访问 postgres 的 shell (psql)

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

docker-compose: accessing postgres' shell (psql)

postgresqldockerdocker-compose

提问by aralar

I'm trying to access PostgreSQL's shell (psql) using docker-compose, but I'm having some difficulties... Here's my docker-composefile:

我正在尝试使用 访问 PostgreSQL 的 shell ( psql) docker-compose,但我遇到了一些困难......这是我的docker-compose文件:

main:
  build: .
  volumes:
    - .:/code
  links:
    - postgresdb
  environment:
    - DEBUG=true


postgresdb:
  build: utils/sql/
  ports:
    - "5432"
  environment:
    - DEBUG=true

I've tried to access psqlby going through the mainas well as the postgresdbservices, by running

我试图访问psql通过该会main还有postgresdb服务,通过运行

docker-compose run postgresdb psql -h postgresdb -U docker mydatabase

docker-compose run postgresdb psql -h postgresdb -U docker mydatabase

but all I get is psql: could not translate host name "postgresdb" to address: Name or service not known... which I don't understand because I use postgresdbas the host in the database configuration file e.g.:

但我得到的只是psql: could not translate host name "postgresdb" to address: Name or service not known......我不明白,因为我postgresdb在数据库配置文件中用作主机,例如:

DB_ACCESS = {
    'drivername': 'postgres',
    # Name of docker-compose service
    'host': 'postgresdb',
    'port': '5432',
    'username': 'docker',
    'password': '',
    'database': 'mydatabase'
}

采纳答案by Vitaly Isaev

You're trying to connect the Postgresdb container from itself, but container is not aware about the alias postgresdbthat you have set up inside your maincontainer.

您正在尝试从自身连接 Postgresdb 容器,但容器不知道postgresdb您在main容器内设置的别名。

Consider the following example:

考虑以下示例:

$ docker run -it --rm ubuntu hostname --fqdn
817450b29b34

Now you see that Postgresdb doesn't know how to resolve postgresdb. But postgresdbshould be resolvable from mainanyway.

现在您看到 Postgresdb 不知道如何解析postgresdb. 但无论如何postgresdb应该可以解决main

You have several opportunities to fix this:

您有几个机会来解决这个问题:

  1. Try to set hostname for Postgresdb explicitly (in your docker-compose.yml);
  2. Access the Postgresdb from maincontainer in such way: $ docker exec -it main_container_full_name psql -h postgresdb -U docker mydatabase
  1. 尝试为 Postgresdb 显式设置主机名(在您的 中docker-compose.yml);
  2. main以这种方式从容器访问 Postgresdb :$ docker exec -it main_container_full_name psql -h postgresdb -U docker mydatabase

回答by l3x

Things have changed a bit since the question was originally asked.

自从最初提出这个问题以来,情况发生了一些变化。

Here's how you can access PostgreSQL's shell (psql) using docker-compose today:

以下是今天使用 docker-compose 访问 PostgreSQL 的 shell (psql) 的方法:

  1. Use a docker-compose.yml version '2' config file
  2. Use 'depends_on' rather than 'links' (new to version '2')
  3. Break up your containers using 'services'
  1. 使用 docker-compose.yml 版本“2”配置文件
  2. 使用“depends_on”而不是“links”(新版本“2”)
  3. 使用“服务”分解您的容器

docker-compoose.yml

docker-compose.yml

version: '2'
services:      
   postgresdb:
     build: utils/sql/
     ports:
       - "5432"
     environment:
       - DEBUG=true
   main:
     build: .
     volumes:
       - .:/code
     depends_on:
       - postgresdb
     environment:
       - DEBUG=true

In your docker engine shell ...

在您的 docker 引擎外壳中...

  1. Find the container id (or container name) from the docker pscommand
  2. Use that id to open a shell prompt
  3. Switch user to the postgres user account
  4. Run psql
  1. docker ps命令中查找容器 ID(或容器名称)
  2. 使用该 ID 打开 shell 提示
  3. 将用户切换到 postgres 用户帐户
  4. 运行 psql

See example below:

请参阅下面的示例:

                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 1.11.2, build HEAD : a6645c3 - Wed Jun  1 22:59:51 UTC 2016
Docker version 1.11.2, build b9f10c9
docker@default:~$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
b563764171e2        poc_web             "bundle exec rails s "   43 minutes ago      Up 43 minutes       0.0.0.0:3000->3000/tcp   poc_web_1
c6e480b0f26a        postgres            "/docker-entrypoint.s"   49 minutes ago      Up 49 minutes       0.0.0.0:5432->5432/tcp   poc_db_1
docker@default:~$ docker exec -it c6e480b0f26a sh
# su - postgres
No directory, logging in with HOME=/
$ psql
psql (9.5.3)
Type "help" for help.

postgres=#

Notes:

笔记:

  1. docker-compose up will start services in dependency order
  2. If you want to stand up a postgres container using the official image, you docker-compose.ymlmight look more like the following:
  1. docker-compose up 将按照依赖顺序启动服务
  2. 如果你想使用官方镜像建立一个 postgres 容器,你的 docker-compose.yml可能看起来更像下面这样:

version: '2' services:
db: image: postgres ports: - "5432:5432" . . .

version: '2' services:
db: image: postgres ports: - "5432:5432" . . .

References

参考

https://docs.docker.com/compose/compose-file/

https://docs.docker.com/compose/compose-file/

回答by Tate Thurston

Let's start with a minimal docker-compose.ymlfile for reference:

让我们从一个最小的docker-compose.yml文件开始以供参考:

version: "3"

services:
  postgres:
    image: postgres:9.5

You bring your services up with docker-compose up.

你提供你的服务docker-compose up

Let's assume you have a database you want to connect to called test.

假设您有一个要连接的数据库,名为test.

Answer:docker-compose run --rm postgres psql -h YOUR_SERVICE -U YOUR_USER -d YOUR_DATABASE

回答:docker-compose run --rm postgres psql -h YOUR_SERVICE -U YOUR_USER -d YOUR_DATABASE

The --rmoption removes the container after exit, so you don't create unnecessary duplicate containers just for accessing the database.

--rm选项会在退出后删除容器,因此您不会仅为访问数据库而创建不必要的重复容器。

In our example, this would be docker-compose run --rm postgres psql -h postgres -U postgres -d test

在我们的例子中,这将是 docker-compose run --rm postgres psql -h postgres -U postgres -d test

Alternatively, you could use psql's connection string: docker compose run --rm postgres psql -d postgres://YOUR_USER@YOUR_SERVICE/YOUR_DATABASE

或者,您可以使用 psql 的连接字符串: docker compose run --rm postgres psql -d postgres://YOUR_USER@YOUR_SERVICE/YOUR_DATABASE

回答by Daniel

The following worked for me:

以下对我有用:

in docker-compose.yaml

docker-compose.yaml 中

services:
  db:
    container_name: db
    image: postgres

then, after running docker-compose up

然后,运行后 docker-compose up

I can run

我可以跑

sudo docker exec -it -u postgres db psql
postgres=#

to get the shell

得到外壳

Notes:

笔记:

  1. I'm using docker-compose version 3
  2. I've given the psql container a name (db). Which allowed me to avoid running docker psto get the container_id
  3. the -u postgresallows me to access the container as the postgressuperuser
  1. 我正在使用 docker-compose 版本 3
  2. 我给 psql 容器起了一个名字(db)。这使我可以避免运行docker ps以获取 container_id
  3. -u postgres允许我进入容器作为Postgres的超级用户