使用 docker-compose 时无法连接到 postgres

2024-03-11

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

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

这是我的 docker-compose.yml

version: '3'

services:
  db:
    image: postgres
    environment:
        POSTGRES_DB: slack
        POSTGRES_USER: username
        POSTGRES_PASSWORD: password
  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=password -d postgres

因为我猜我不能用 docker-compose 做同样的事情,提前谢谢。


主机名应该是 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

在这种情况下,

主机名:db

端口:5432

用户: 雪花

通过:1步关闭

希望这可以帮助 :)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 docker-compose 时无法连接到 postgres 的相关文章

随机推荐

  • WPF 中的报表查看器

    我有一个WPF项目 我需要预览水晶报表 是否有可能做到这一点 我在 WPF 项目中找不到报表查看器控件 thanks 您可以使用CrystalReportViewerWindows 窗体与 Windows 窗体互操作技术的结合 将查看器添加
  • PhpDocs:链接到“@deprecated”标签描述中的另一个方法?

    是否可以链接到另一个方法 类 属性 等 在我的项目内联内 deprecated标签 像这样 Method description deprecated 1 0 Reason for deprecation use link newMetho
  • QXcbIntegration:无法创建平台 OpenGL 上下文,GLX 和 EGL 均未启用

    我有一个用 QT 和 OpenGL 构建的 unix 二进制文件 我试图在 linux 64 上执行它 它是一个简单的可视化程序 显示 2d 和 3d 图形 我已经安装了所有必需的依赖项 例如 QT 和 openGL 库 但是 我在尝试执行
  • Pygame 根据位置重叠精灵(绘制顺序)

    总的来说 我对 Pygame 和 Python 还比较陌生 所以希望这不是太陌生 I m making a top down RPG and I have two Sprite objects with images that look f
  • 为 click.options() 创建包装装饰器

    我正在尝试为点击装饰器创建一个包装装饰器 click options foo required True import click def foo option func orig decorator click option foo re
  • 使用相同Keycloak的2个APP的身份验证(SSO)

    我需要您针对特殊用例的帮助 我在互联网上搜索但没有找到任何适合我的情况的内容 我是 keycloak 的新手 所以这可能很容易做到 但我未能解决它 我有两个具有 Spring Boot 安全性的应用程序 它们连接到同一个 keycloak
  • Photoshop 抠图滤镜是如何实现的?

    Photoshop 有很多很酷的功能艺术滤镜 http alanwho com photoshop photoshop artistic filters 我很想了解底层算法 一种特别有趣的算法是 Cutout 过滤器 上面链接中的编号 2
  • PostgreSQL 子查询使用 like

    假设我有第一张这样的桌子 分支表 name description 123456ABC FOO 553646DEF FO2 第二个表是这样的 余额表 name description ABC oof DEF 2of 我想查询余额表 其中每一
  • Windows Server 2008 的传真服务器替代方案

    为了在内部发送传真 我们一直使用以下 Coldfusion 代码来创建处理传真的对象
  • 查找 google 何时自动完成返回零结果

    我使用谷歌地点自动完成功能来提供地点建议 现在这就是我想要的 当用户输入不在谷歌数据库中的位置时 即谷歌返回零结果时 我需要能够捕获零结果事件 并触发我的客户自动完成功能 当谷歌返回零结果时我如何找到 如果结果为零 autocomplete
  • 构建 Apk 时出错:com.android.build.api.transform.TransformException:生成主 dex 列表时出错

    在项目到两个库模块和一个应用程序模块之后 Android Studio 无法构建 Apk 但是当我将项目构建到真实设备或模拟器中时 它可以完美运行 这是我在 消息 选项卡中遇到的完整堆栈错误 FAILURE Build failed wit
  • 是什么导致 HttpListener HTTP 503 错误?

    因此 我们有一个大型程序 它使用 HttpListener 来实现小型远程管理功能 由于我不明白的原因 有些人遇到了 503 错误的问题 由于我们没有提供错误 因此框架中发生了一些事情 现在 我的问题是 框架内部是什么导致了这个错误 是前缀
  • 创建 .p12 文件

    Using openssl 我创建了一个私钥 如下所示 openssl genrsa out myKey pem 然后 生成csr应CA的要求 我已执行以下操作 openssl req new key myKey pem out cert
  • 如何将 Istio Service Mesh 从 http 升级到 http2?

    我们使用 Kubernetes 并使用 Istio Service Mesh 目前 网关中存在针对 HTTPS 的 SSL 终止 我在 istio proxy 日志中看到 HTTP 协议是 HTTP 1 1 由于 HTTP2 的各种优点 我
  • 如何在python的tkinter中设置Treeview的宽度

    最近 我使用tkinter TreeView显示许多列Python 具体来说 树视图中有 49 列数据 我用grid管理我的小部件 我发现树视图的宽度仅取决于列的宽度 我的问题是 如何设置 Treeview 的宽度 默认宽度是所有列宽度的总
  • 循环地址对齐如何影响 Intel x86_64 上的速度?

    我发现相同的 C 代码编译为完全相同的机器指令但位于不同对齐的地址时性能下降了 15 当我的小主循环从 0x415220 开始时 它比从 0x415250 开始时要快 我在 Intel Core2 Duo 上运行它 我在 x86 64 Ub
  • 在 Mac OS 下设置 COBOL 编译器?

    在我的大学里 我们正在学习 COBOL 我正努力在明年上课之前抢先学习 COBOL C 和 Java 问题是 COBOL 是so旧的 很难在 mac 中获得对它的支持 我的笔记本电脑是 mac 我知道它在 Mac 上运行良好 但找到可以解释
  • 如何以全屏模式启动 NSWindow

    我正在 Xcode 5 中为 Macos 开发一个应用程序 我想全屏显示我的第一个窗口 没有工具栏 只有我的视图 我找到了一种在角落显示全屏按钮的方法 AppDelegate m void applicationDidFinishLaunc
  • BCL(基类库)与 FCL(框架类库)

    两者有什么区别 我们可以互换使用它们吗 基类库 BCL 字面意思就是基础 它包含基本的基本类型 例如System String and System DateTime 框架类库 FCL 是更广泛的库 包含全部内容 ASP NET WinFo
  • 使用 docker-compose 时无法连接到 postgres

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