无法通过Postico连接到PostgreSQL Docker容器

7
我正试图使用Postico连接到本地机器上的Docker PostgreSQL容器。我尝试连接0.0.0.0、localhost和127.0.0.1,但是每次都会出现以下错误:
could not connect to server: Connection refused
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
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?

0.0.0.0 给我一个类似但更小的错误:

could not connect to server: Connection refused
    Is the server running on host "0.0.0.0" and accepting
    TCP/IP connections on port 5432?

这是我的Docker Compose文件:

version: '3'
services:
  prisma:
    image: prismagraphql/prisma:1.23
    restart: always
    ports:
    - "4466:4466"
    environment:
      PRISMA_CONFIG: |
        port: 4466
        databases:
          default:
            connector: postgres
            host: postgres
            port: 5432
            user: prisma
            password: prisma
            migrations: true
  postgres:
    image: postgres:10.5
    restart: always
    environment:
      POSTGRES_USER: prisma
      POSTGRES_PASSWORD: prisma
    volumes:
      - postgres:/var/lib/postgresql/data
volumes:
  postgres:

感谢 Egor 提供的解决方案!我忘记在我的 docker-compose 文件中指定 ports: - "5432:5432"。这是初学者的错误;)


好的,很棒。我刚刚更新了 docker-compose.yml 文件中的端口,对我也起作用了!谢谢! - joshlsullivan
2个回答

14

我也曾遇到使用Postico连接Docker容器中的Postgres DB时出现问题。

最终,我的问题是因为我本地运行了一个Postgres DB

一旦我断开了本地的Postgres DB,我就能够使用Postico连接到我的docker DB。将主机设置为localhost,并使用在我的docker-compose.yml文件中定义的POSTGRES_USERPOSTGRES_PASSWORD和主机端口。

Postico连接详细信息


我的Postgres实例是通过homebrew运行的。使用“brew services stop postgresql”命令解决了这个问题。 - michaelmichael
啊,这已经咬了我三次了,pg重启而我没有注意到。谢谢你的发布。 - Liz

5
如果Postgres版本不重要,请尝试将Postgres镜像更改为这个,它对我有效。
还要确保在docker-compose.yml中添加端口。
postgres:
    image: postgres
    restart: always
    environment:
      POSTGRES_USER: prisma
      POSTGRES_PASSWORD: prisma
    ports: 
     - "5432: 5432"
    volumes:
      - postgres:/var/lib/postgresql/data

附注:刚刚更新了答案,以提高可读性。


似乎那也不起作用。我仍然在使用postgres:latest时收到“连接被拒绝”的错误。 - Jon Leopard
你在Postgres的docker-compose中添加了端口吗? ports: - "5432:5432" - Yegor Zaremba

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接