【问题标题】:How do I connect to Docker Postgres Container from an Outside CLI?如何从外部 CLI 连接到 Docker Postgres 容器?
【发布时间】:2021-08-02 18:25:49
【问题描述】:

我的 Postgres 容器正在运行,由这个 docker-compose 文件构建:

version: "3.9"

services:
    db:
        image: postgres
        volumes:
            - ./data/db:/var/lib/postgresql/data
        ports:
            - "5432:5432"
        environment:
            - POSTGRES_DB=db
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=password.

它运行良好,我的其他 dockerized 服务器可以连接到它。但是,如果我从 docker 实例外部打开 CLI 并尝试连接

psql postgres://postgres:password@localhost:5432/db

或者尝试在PyCharm中添加数据库连接,我明白了

psql: error: could not connect to server: FATAL:  database "db" does not exist

作为回应。我需要做什么才能允许外部调用容器化数据库?我尝试在 docker compose 中添加“expose:5432”,但这没有帮助。

【问题讨论】:

  • 看起来问题与您尝试连接的方式有关,也许您的数据库名称不是db 尝试连接并列出数据库并相应地更改连接字符串。也许试试postgresql://postgres:password@localhost:5432`
  • 我也在本地运行 pgadmin,它在 5432 上。这会干扰它吗?我不知道如何避免这种情况

标签: postgresql docker docker-compose dockerfile docker-container


【解决方案1】:

在这里回答了我自己的问题 - 通过阻止 PgAdmin 运行,我猜这会阻塞 5432 端口,容器化的 postgres 服务可以通过 CLI 和 PyCharm 访问

【讨论】:

    猜你喜欢
    • 2015-11-28
    • 1970-01-01
    • 2016-07-20
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    • 2019-03-16
    相关资源
    最近更新 更多