【问题标题】:Can't connect to postgres when using docker-compose使用 docker-compose 时无法连接到 postgres
【发布时间】:2018-01-18 20:37:50
【问题描述】:

我是 docker 新手,还在学习如何使用它, 我正在尝试使用 docker-compose 一起运行 Django 和 Postgres 他们运行完美,迁移完成,一切顺利,但我有一个问题,我无法使用 pgAdmin4 连接到数据库来查看数据库 这是我的设置。py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'slack',
        'USER': 'snowflake',
        'PASSWORD': '1Stepcloser',
        'HOST': 'db',
        'PORT': 5432,
    }
}

这就是我的 docker-compose.yml

version: '3'

services:
  db:
    image: postgres
    environment:
        POSTGRES_DB: slack
        POSTGRES_USER: snowflake
        POSTGRES_PASSWORD: 1Stepcloser
  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/slack_code
    ports:
      - "8000:8000"
    depends_on:
      - db

一切正常:

sudo docker-compose up
slackwebapp_db_1 is up-to-date
Creating slackwebapp_web_1 ... done
Attaching to slackwebapp_db_1, slackwebapp_web_1
db_1   | The files belonging to this database system will be owned by user "postgres".
db_1   | This user must also own the server process.
db_1   | 
db_1   | The database cluster will be initialized with locale "en_US.utf8".
db_1   | The default database encoding has accordingly been set to "UTF8".
db_1   | The default text search configuration will be set to "english".
db_1   | 
db_1   | Data page checksums are disabled.
db_1   | 
db_1   | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db_1   | creating subdirectories ... ok
db_1   | selecting default max_connections ... 100
db_1   | selecting default shared_buffers ... 128MB
db_1   | selecting dynamic shared memory implementation ... posix
db_1   | creating configuration files ... ok
db_1   | running bootstrap script ... ok
db_1   | performing post-bootstrap initialization ... ok
db_1   | syncing data to disk ... ok
db_1   | 
db_1   | Success. You can now start the database server using:
db_1   | 
db_1   |     pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1   | 
db_1   | 
db_1   | WARNING: enabling "trust" authentication for local connections
db_1   | You can change this by editing pg_hba.conf or using the option -A, or
db_1   | --auth-local and --auth-host, the next time you run initdb.
db_1   | waiting for server to start....2018-01-18 19:46:43.851 UTC [38] LOG:  listening on IPv4 address "127.0.0.1", port 5432
db_1   | 2018-01-18 19:46:43.851 UTC [38] LOG:  could not bind IPv6 address "::1": Cannot assign requested address
db_1   | 2018-01-18 19:46:43.851 UTC [38] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
db_1   | 2018-01-18 19:46:43.853 UTC [38] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2018-01-18 19:46:43.864 UTC [39] LOG:  database system was shut down at 2018-01-18 19:46:43 UTC
db_1   | 2018-01-18 19:46:43.867 UTC [38] LOG:  database system is ready to accept connections
db_1   |  done
db_1   | server started
db_1   | CREATE DATABASE
db_1   | 
db_1   | CREATE ROLE
db_1   | 
db_1   | 
db_1   | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1   | 
db_1   | 2018-01-18 19:46:44.388 UTC [38] LOG:  received fast shutdown request
db_1   | waiting for server to shut down....2018-01-18 19:46:44.389 UTC [38] LOG:  aborting any active transactions
db_1   | 2018-01-18 19:46:44.390 UTC [38] LOG:  worker process: logical replication launcher (PID 45) exited with exit code 1
db_1   | 2018-01-18 19:46:44.391 UTC [40] LOG:  shutting down
db_1   | 2018-01-18 19:46:44.402 UTC [38] LOG:  database system is shut down
db_1   |  done
db_1   | server stopped
db_1   | 
db_1   | PostgreSQL init process complete; ready for start up.
db_1   | 
db_1   | 2018-01-18 19:46:44.501 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2018-01-18 19:46:44.501 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2018-01-18 19:46:44.502 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2018-01-18 19:46:44.514 UTC [65] LOG:  database system was shut down at 2018-01-18 19:46:44 UTC
db_1   | 2018-01-18 19:46:44.518 UTC [1] LOG:  database system is ready to accept connections
web_1  | Performing system checks...
web_1  | 
web_1  | System check identified no issues (0 silenced).
web_1  | 
web_1  | You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
web_1  | Run 'python manage.py migrate' to apply them.
web_1  | January 18, 2018 - 19:48:49
web_1  | Django version 2.0.1, using settings 'slack_webapp.settings'
web_1  | Starting development server at http://0.0.0.0:8000/
web_1  | Quit the server with CONTROL-C.
web_1  | [18/Jan/2018 19:56:03] "GET / HTTP/1.1" 200 16559
web_1  | [18/Jan/2018 19:56:03] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
web_1  | [18/Jan/2018 19:56:04] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 82564
web_1  | [18/Jan/2018 19:56:04] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 80304
web_1  | [18/Jan/2018 19:56:04] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 81348
web_1  | [18/Jan/2018 19:56:08] "GET /admin/ HTTP/1.1" 302 0
web_1  | [18/Jan/2018 19:56:09] "GET /admin/login/?next=/admin/ HTTP/1.1" 200 1855
web_1  | [18/Jan/2018 19:56:09] "GET /static/admin/css/base.css HTTP/1.1" 200 16106
web_1  | [18/Jan/2018 19:56:09] "GET /static/admin/css/responsive.css HTTP/1.1" 200 17894
web_1  | [18/Jan/2018 19:56:09] "GET /static/admin/css/login.css HTTP/1.1" 200 1203
web_1  | [18/Jan/2018 19:58:58] "POST /admin/login/?next=/admin/ HTTP/1.1" 302 0
web_1  | [18/Jan/2018 19:58:58] "GET /admin/ HTTP/1.1" 200 2988
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/css/base.css HTTP/1.1" 304 0
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/css/dashboard.css HTTP/1.1" 200 412
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/css/responsive.css HTTP/1.1" 304 0
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/css/fonts.css HTTP/1.1" 304 0
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 304 0
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 304 0
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 304 0
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/img/icon-addlink.svg HTTP/1.1" 200 331
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/img/icon-changelink.svg HTTP/1.1" 200 380
web_1  | [18/Jan/2018 19:59:05] "GET /admin/ HTTP/1.1" 200 2988
web_1  | [18/Jan/2018 19:59:07] "GET /admin/ HTTP/1.1" 200 2988
web_1  | [18/Jan/2018 19:59:11] "GET /admin/ HTTP/1.1" 200 2988
^CGracefully stopping... (press Ctrl+C again to force)
Stopping slackwebapp_web_1 ... done
Stopping slackwebapp_db_1  ... done

但我仍然无法连接,而且我不知道如何像我们在

中那样为 Postgres 默认用户设置密码
sudo docker run --name test -e POSTGRES_PASSWORD=1Stepcloser -d postgres

因为我想我不能用 docker-compose 做同样的事情,在此先感谢。

【问题讨论】:

  • 这个答案为我解决了这个问题:*.com/a/62230446/3559330 - 将主机名更改为“db”,即数据库服务的名称。

标签: docker docker-compose pgadmin-4


【解决方案1】:

主机名应该是您的 docker-compose.yml 中定义的服务名称

这是因为你在 docker 网络中

这里不能使用 localhost 或 127.0.0.1,因为 pgadmin 在容器中,这里的 localhost 表示 'pgadmin 容器'。

让我们考虑一下你的情况:

version: '3'

services:
  db:
    image: postgres
    ports:
      - 5432:5432
    environment:
        POSTGRES_DB: slack
        POSTGRES_USER: snowflake
        POSTGRES_PASSWORD: 1Stepclose

  pgadmin:
    image: chorss/docker-pgadmin4
    ports:
      - 5050:5050

在这种情况下,

主机名:数据库

端口:5432

用户:雪花

通过:1Stepclose

希望这会有所帮助:)

【讨论】:

  • 将主机名更改为 db 对我有用,哇,多么微妙的问题。非常感谢!!
【解决方案2】:

为了从外部程序访问 postgres 数据库,您需要将 postgres 服务公开的端口 5432 挂载到主机上的端口。 通过对 docker-compose.yml 文件进行以下更改,您应该能够使用 pgadmin(在 localhost:5432 上)连接到数据库,并让 postgres 为您创建用户。

db:
  image: postgres
  ports:
  - "5432:5432"
  environment:
    - POSTGRES_DB=slack
    - POSTGRES_USER=snowflake
    - POSTGRES_PASSWORD=1Stepclose

编辑: 我没有意识到您正在尝试连接在另一个 docker 容器中运行的 pgadmin4。设置它以允许 pgadmin4 容器与 postgres 容器通信的最简单方法是将 pgadmin 作为服务添加到 docker-compose.yml 文件中。更新您的 docker-compose.yml 文件以包含以下配置:

version: '3'

services:
  db:
    image: postgres
    ports:
      - 5432:5432
    environment:
        POSTGRES_DB: slack
        POSTGRES_USER: snowflake
        POSTGRES_PASSWORD: 1Stepclose

  pgadmin:
    image: chorss/docker-pgadmin4
    ports:
      - 5050:5050

  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/slack_code
    ports:
      - "8000:8000"
    depends_on:
      - db

浏览到localhost:5050 > 点击添加新服务器 > 输入任何名称 > 点击连接选项卡 > 输入db 作为主机名/地址 > 输入snowflake 作为用户名 > 输入1Stepclose 作为密码 > 点击保存

【讨论】:

  • 我试过你的方法还是不行,是不是因为 docker-compose 的缘故!
  • 在主机上运行sudo apt-get install postgresql-client(当docker-compose up 正在运行时),然后从终端psql -U snowflake -h localhost -p 5432 -d slack 运行以下命令,如果可行,则意味着您的pgadmin4 程序配置不正确。
  • 顺便说一句,当您同时设置 POSTGRES_USER 和 POSTGRES_PASSWORD 变量时,您正在创建一个新的 postgres 用户并且没有设置默认 postgres 用户的密码,只有当您只设置 POSTGRES_PASSWORD 变量时,它才会将其设置为默认用户的密码。
  • 我只设置了 POSTGRES_PASSWORD 但似乎也无法正常工作,但是,我放弃并按原样使用它,因为这一切的主要原因是学习 Django,而不是 docker,但我认为它有一个设置良好的环境是个好主意。
  • 我已经更新了我的答案。如果有效,请将我的答案标记为正确。
【解决方案3】:

在您的 setting.py 文件中,您将数据库主机称为“db”,但在您的撰写输出中,它似乎被称为“slackwebapp_db_1”。尝试将 setting.py 更改为全名。

【讨论】:

  • 'slackw3bapp_db_1' 是容器名称伙伴,它由 docker-compose 自动生成,加上 Django 和 postgres 通信良好,我只想使用 3rd 方应用程序查看数据库以了解我是什么做
【解决方案4】:

我使用Pycharm控制我的项目,并且Pycharm内置的数据库管理系统已经连接成功,可能是pgadmin4有问题,我是使用chorss / docker-pgadmin4图像作为pgAdmin4,因为我是linux所以也许是图像有问题或其他什么。谢谢大家的努力。

【讨论】:

    【解决方案5】:

    我查看了postgres,从中找到了git repository(根据您的docker-compose,您使用的是最新标签)。看起来默认用户名和密码“postgres”是硬编码的。您可能想在 settings.py 中尝试使用“postgres”获取用户名和密码,看看是否可行。

    【讨论】:

    • 我试过了,但不幸的是也不起作用,奇怪的是当我使用以下命令加载 posgress 容器时:“sudo docker run --name test -e POSTGRES_PASSWORD=1Stepcloser -d postgres”但是当使用 docker-compose 时我无法连接。
    • 对不起我之前的废话回答。我尝试了类似的事情,这应该可以。一个区别是我不在 docker-compose.yml 中启动 runserver 命令。我使用 -d 启动 docker-compose,然后附加到 db 容器以检查存在的环境变量。然后我附加到 Web 容器并运行迁移命令。我注意到数据库松弛是在 db 容器中创建的,所以连接应该可以工作。我注意到在您的日志输出中,有人抱怨您也应该执行 migrate 命令。也许你应该这样做......