【问题标题】:Unable to connect my postgres database using Docker无法使用 Docker 连接我的 postgres 数据库
【发布时间】:2021-04-20 22:26:09
【问题描述】:

我正在使用 Docker,但它不起作用。我不明白为什么...

这是我的 docker-compose.yml :

# docker-compose.yml
version: '3'
services:
  database:
    image: "postgres" # use latest official postgres version
    env_file:
      - database.env # configure postgres
    volumes:
      - database-data:/var/lib/postgresql/data/ # persist data even if container shuts down
volumes:
  database-data: # named volumes can be managed easier using docker-compose

这是我的 database.env :

# database.env
POSTGRES_USER=admin
POSTGRES_PASSWORD=pass
POSTGRES_DB=db

我输入了:

docker-compose up

我明白了:

2021-01-15 19:57:11.683 UTC [1] LOG: database system is ready to accept connections

这样很好。

那么:

docker-compose run database bash

我试过了:

su - postgres

psql --host=localhost --username=admin --dbname=db

但我明白了:

psql: error: could not connect to server: Connection refused

Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? could not connect to server: Cannot assign requested address

Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?

而如果我在终端 linux 中输入:

sudo su -

su - postgres

psql --host=localhost --username=admin --dbname=db

我必须输入密码,它可以工作......

我不知道怎么解决...

你能帮帮我吗?

非常感谢! `

【问题讨论】:

    标签: postgresql docker docker-compose dockerfile


    【解决方案1】:

    docker-compose run 启动一个新容器。在那个容器中,localhost 是那个容器,而不是另一个已经在运行的数据库容器。如Networking in Compose 中所述,您可以使用docker-compose.yml 文件中现有数据库容器的服务名称作为主机名;所以连接到数据库的一种方法是

    sudo \                         # launching containers can require root permission
    docker-compose run database \  # start a new container
    psql \                         # with psql as the main command
      --host=database \            # connecting to the existing database container
      --username=admin \
      --dbname=db
    

    假设您已从容器中发布 ports:

    services:
      database:
        image: postgres
        ports:         # These two lines are very routine;
          - 5432:5432  # do you already have them?
    

    那么您也可以使用主机的 IP 地址和第一个 ports: 号码连接到数据库。这与直接在主机上运行数据库服务器没有区别。由于您通过 TCP 连接来连接数据库,因此您无需成为任何特定用户即可。

    # without needing sudo at all
    psql \                # launch the client directly on the host
      --host=localhost \  # connecting to a database on the same machine
      --username=admin \
      --dbname=db
    # --port=5432         # needs to match first ports: number; this is the default
    

    (您可能会想到docker-compose exec,它是一种调试工具,可以获取shell 或以其他方式对正在运行的容器运行命令。不过,您通常不需要它来与数据库交互;您只需需要一个普通的客户端库或命令行工具,例如psql。)

    【讨论】:

    • 谢谢,但是当我输入 sudo \ # launching containers can require root permission docker-compose run database \ # start a new container psql \ # with psql as the main command --host=database \ # connecting to the existing database container --username=admin \ --dbname=db 时,我得到了同样的错误:Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?
    • 这意味着它没有看到--host=database 选项。尝试删除所有换行符和 cmets 并重新输入,sudo docker-compose run database psql ...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    相关资源
    最近更新 更多