首页 > 解决方案 > 带有端口映射的 docker-compose 中的 iOS postgres

问题描述

我有以下码头工人组成:

version: '3'
services:
  postgres:
  container_name: test-psql
  image: postgres:10.4-alpine
  restart: always
  ports:
    - 5432:5432
  volumes:
    - ~/docker_data/postgres:/data/postgres
  environment:
    POSTGRES_USER: test
    POSTGRES_PASSWORD: test
    POSTGRES_DB: test

我开始一切,docker-compose up然后docker ps

CONTAINER ID    IMAGE         COMMAND         CREATED      STATUS       PORTS         NAMES
f4cae94afaa6    postgres:10.4-alpine   "docker-entrypoint.s…"   48 seconds ago    Up 47 seconds    0.0.0.0:5432->5432/tcp   test-psql

但是当试图连接到它时psql

psql -h localhost -U test

我得到错误

psql: 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?

标签: iospostgresqldockerpsql

解决方案


你正在尝试的是正确的 - 我在我的最后测试了它以防万一它按预期工作。我建议检查您的防火墙配置。


推荐阅读