我在服务器上的docker容器中有Postgresql。我如何从外部,也就是从我的本地计算机连接到它?我应该应用什么设置来允许这样做?


当前回答

我尝试从localhost (mac)连接到postgres容器。我将docker-compose文件中的端口从5432更改为3306,并启动了容器。不知道我为什么这么做:|

然后我尝试通过PSequel和adminer连接到postgres,但连接无法建立。

切换回端口5432后,一切工作正常。

  db:
    image: postgres
    ports:
      - 5432:5432
    restart: always
    volumes:
      - "db_sql:/var/lib/mysql"
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: password
      POSTGRES_DB: postgres_db

这就是我想要分享的经验。也许有人可以利用它。

其他回答

这里有很好的答案,但是如果您想要postgres数据库管理的一些接口,您可以在本地计算机上安装pgAdmin,并使用其IP和postgres公开端口(默认为5432)连接到远程计算机。

我尝试从localhost (mac)连接到postgres容器。我将docker-compose文件中的端口从5432更改为3306,并启动了容器。不知道我为什么这么做:|

然后我尝试通过PSequel和adminer连接到postgres,但连接无法建立。

切换回端口5432后,一切工作正常。

  db:
    image: postgres
    ports:
      - 5432:5432
    restart: always
    volumes:
      - "db_sql:/var/lib/mysql"
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: password
      POSTGRES_DB: postgres_db

这就是我想要分享的经验。也许有人可以利用它。

我设法让它在linux上运行

运行docker postgres -确保端口被发布,我使用alpine是因为它是轻量级的。 docker run——rm -P 127.0.0.1:5432:5432 -e POSTGRES_PASSWORD="1234"——name pg postgres:alpine 使用另一个终端,使用postgres uri从主机访问数据库 psql postgresql: / / postgres: 1234 @localhost: 5432 / postgres

MAC用户请将PSQL替换为pgcli

首先打开postgres的docker映像

docker exec -it <container_name>

然后你会得到根目录--root@868594e88b53:/# 它需要数据库连接

psql postgresql://<username>:<databasepassword>@postgres:5432/<database>

我在Docker容器中使用django和postgres。在docker-compose文件中,添加以下内容:

db:
    image: postgres:10-alpine
    environment:
        - POSTGRES_DB=app
        - POSTGRES_USER=postgres
        - POSTGRES_PASSWORD=supersecretpassword
    ports:
        - "6543:5432"

这个端口设置使用了本地机器可以访问的端口6543(它只需要不同于5432)。就我自己而言,我将DBeaver连接到它。这将防止您的应用程序请求和本地机器请求之间的端口冲突。

起初,我收到一条消息,说端口5432正在使用(由django应用程序),所以我无法通过pgAdmin或DBeaver访问。