postgresql docker postgres pgadmin 本地连​​接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25540711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:45:28  来源:igfitidea点击:

docker postgres pgadmin local connection

postgresqldockerpgadmin

提问by Rigoxls

I have created an ubuntu image with nginx, php and postgres.

我用 nginx、php 和 postgres 创建了一个 ubuntu 图像。

I want to connect the postgres database in my current image with pgadminlocated on my local machine.

我想将当前图像中的 postgres 数据库连接到pgadmin位于我的本地机器上。

I have tried using docker inspector to try to use the image ip to make a connection with my local pgadmin but without much success. I've also tried configuring some ports on local to make connection work.

我曾尝试使用 docker inspector 尝试使用图像 ip 与我的本地 pgadmin 建立连接,但没有取得多大成功。我还尝试在本地配置一些端口以使连接正常工作。

回答by stack.it

It's a valid question don't know why people feel "doesn't seem possible to answer that question".

这是一个有效的问题,不知道为什么人们觉得“似乎不可能回答那个问题”。

So, here's how I do what you are trying to do:

所以,这就是我如何做你想做的事情:

  1. Pull postgres image from Docker Hub

    docker pull postgres:latest

  2. Run the container using the below command

    docker run -p 5432:5432 postgres

  3. Using docker's inspect command find the IP

  4. Use that IP, PORT, Username, and Password to connect in PGADMIN

  5. You can also do a simple telnet like below to confirm if you can access docker postgres container:

    telnet IP_ADDRESS 5432

  1. 从 Docker Hub 拉取 postgres 镜像

    docker pull postgres:latest

  2. 使用以下命令运行容器

    docker run -p 5432:5432 postgres

  3. 使用docker的inspect命令查找IP

  4. 使用该 IP、端口、用户名和密码在 PGADMIN 中连接

  5. 您还可以执行如下简单的 telnet 来确认您是否可以访问 docker postgres 容器:

    telnet IP_ADDRESS 5432

回答by Maosheng Wang

What I have done success on windows 10 running docker for windows 1.12.6(9655), the step is like below:

我在 Windows 10 上成功运行 docker for windows 1.12.6(9655),步骤如下:

  1. Pull the latest postgres

    docker pull postgres:latest

  2. run the postgres containner:

    docker run -d -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password123 --name db-my -p 5432:5432 --restart=always postgres

  3. Then installed the latest version of pgAdmin4 from pgadmin website

  4. Run pgAdmin 4 create new server, and input as following Host: 127.0.0.1 Port: 5432 User name: user password: password123

  5. Then everything is ok, connect to docker postgres instance success.
  1. 拉取最新的postgres

    docker pull postgres:latest

  2. 运行 postgres 容器:

    docker run -d -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password123 --name db-my -p 5432:5432 --restart=always postgres

  3. 然后从pgadmin 网站安装最新版本的 pgAdmin4

  4. 运行pgAdmin 4 create new server,输入如下主机:127.0.0.1 端口:5432 用户名:用户密码:password123

  5. 然后一切正常,连接docker postgres实例成功。

回答by Erik Vullings

Alternatively, you could combine Postgres and Pgadmin in one docker-composefile, and login as user [email protected], pwd: admin. To add the Posgres server, use hostname postgres, port 5432.

或者,您可以将 Postgres 和 Pgadmin 组合在一个docker-compose文件中,并以 user [email protected], pwd:身份登录admin。要添加 Posgres 服务器,请使用 hostname postgres, port 5432

version: '3'
services:
  postgres:
    image: postgres
    hostname: postgres
    ports:
      - "6543:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: TEST_SM
    volumes:
      - postgres-data:/var/lib/postgresql/data
    restart: unless-stopped

  pgadmin:
    image: dpage/pgadmin4
    depends_on:
      - postgres
    ports:
      - "5555:80"
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: admin
    restart: unless-stopped

volumes:
  postgres-data:

回答by Thavaprakash Swaminathan

This is what i did

这就是我所做的

for postgres

对于 postgres

docker run -p 5432:5432  --name container-postgresdb -e POSTGRES_PASSWORD=admin -d postgres

for pgadmin

对于 pgadmin

docker run -p 5050:80  -e "[email protected]" -e "PGADMIN_DEFAULT_PASSWORD=admin"  -d dpage/pgadmin4

Connection string for pgadmin

pgadmin 的连接字符串

host: host.docker.internal
database: postgres
user: postgres
password: admin

It works fine!!!!!

它工作正常!!!!

回答by Aleksandr Makov

If pgAdmin is intended to be run wihtin same Ubuntu host/guest, then you need to link postgres container, so it could be resolved by a name.

如果 pgAdmin 打算在相同的 Ubuntu 主机/来宾中运行,那么您需要链接 postgres 容器,以便可以通过名称解析它。

1. Run a postgres container:

1. 运行一个 postgres 容器:

docker run --name some-postgres -e POSTGRES_PASSWORD=postgres -d postgres

2. Run pgAdmin container:

2. 运行 pgAdmin 容器:

docker run -p 80:80 --link some-postgres -e "[email protected]" -e "PGADMIN_DEFAULT_PASSWORD=postgres" -d dpage/pgadmin4

3. Now when adding new server in phAdmin web app, use some-postgresas server name

3. 现在在 phAdmin web 应用程序中添加新服务器时,使用some-postgres作为服务器名称

Note the --link some-postgreswhen we were bringing up the pgAdmin. This command makes postgres container visible to pgAdmin container.

请注意--link some-postgres我们何时启动 pgAdmin。此命令使 postgres 容器对 pgAdmin 容器可见。

回答by SolidSnake

In my case I could solve the problem inspecting my postgre image through command

就我而言,我可以通过命令解决检查我的 postgre 图像的问题

docker inspect CONTAINER_ID  | grep IPAddress.

So, I used the docker ip address to config my pgadmin 4 connection and everything was fine. Thanks to @Afshin Ghazi

所以,我使用 docker ip 地址来配置我的 pgadmin 4 连接,一切都很好。感谢@Afshin Ghazi

回答by Afshin Ghazi

I included this in the docker yaml file to get the database and pgAdmin:

我将其包含在 docker yaml 文件中以获取数据库和 pgAdmin:

database:
    image: postgres:10.4-alpine
    container_name: kafka-nodejs-example-database
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    expose:
      - "5432"
    ports:
      - 8000:5432
    volumes:
      - ./services/database/schema.sql:/docker-entrypoint-initdb.d/1-schema.sql
      - ./services/database/seed.sql:/docker-entrypoint-initdb.d/2-seed.sql
  pgadmin:
    image: dpage/pgadmin4
    ports:
      - 5454:5454/tcp
    environment:
      - [email protected]
      - PGADMIN_DEFAULT_PASSWORD=postgres
      - PGADMIN_LISTEN_PORT=5454

The postgres username is alphaone and the password is xxxxxxxxxxx.

postgres 用户名是 alphaone,密码是 xxxxxxxxxxx。

Do a docker psto get the container id and then docker inspect <dockerContainerId> | grep IPAddress

做一个docker ps来获取容器ID,然后docker inspect <dockerContainerId> | grep IPAddress

eg: docker inspect 2f50fabe8a87 | grep IPAddress

例如: docker inspect 2f50fabe8a87 | grep IPAddress

Insert the Ip address into pgAdmin and the database credentials used in docker:

将 IP 地址插入 pgAdmin 和 docker 中使用的数据库凭据:

pgAdmin

管理员

回答by hizmarck

In my case, I had a PostgreSQL container, so I didn't change my container or create a docker-compose approach, I needed pgadming after few months to had installed PostgreSQL, so this is my approach:

就我而言,我有一个 PostgreSQL 容器,所以我没有更改我的容器或创建 docker-compose 方法,几个月后我需要 pgadming 安装 PostgreSQL,所以这是我的方法:

  1. docker inspect my_container_id_postgreSQL
  2. The network assigned to PostgreSQL was:

    "Networks": { "docker_default": { "IPAddress": "172.18.0.2", ... } }

  3. Ran my PGADMIN with --networkcommand.

    docker run -p 85:80 --network docker_default-e '[email protected]' -e 'PGADMIN_DEFAULT_PASSWORD=SuperSecret' -d dpage/pgadmin4

  4. Insert the Ip address into pgAdmin and the database credentials used in docker.

  1. docker inspect my_container_id_postgreSQL
  2. 分配给 PostgreSQL 的网络是:

    "网络": { "docker_default": { "IPAddress": "172.18.0.2", ... } }

  3. --network命令运行我的 PGADMIN 。

    docker run -p 85:80 --network docker_default-e '[email protected]' -e 'PGADMIN_DEFAULT_PASSWORD=SuperSecret' -d dpage/pgadmin4

  4. 将 IP 地址插入 pgAdmin 和 docker 中使用的数据库凭据。

I hope this can be useful for someone. Regards.

我希望这对某人有用。问候。

回答by meij

You have to expose the postgres port in the container to you local system. You do this by running your container like this:

您必须将容器中的 postgres 端口公开给您的本地系统。你可以通过像这样运行你的容器来做到这一点:

docker run -p 5432:5432 <name/id of container>

docker run -p 5432:5432 <name/id of container>

when connecting with your GUI client or CLI make sure to use the ip-address not localhost even if your ip-address is the localhost ip-address.

与 GUI 客户端或 CLI 连接时,请确保使用 ip-address 而不是 localhost,即使您的 ip-address 是 localhost ip-address。

docker pswould give you the ip address your postgres container is on.

docker ps会给你你的 postgres 容器所在的 IP 地址。

回答by Haroon Khalid

In order or find your IP of the container, use following command

为了或查找容器的 IP,请使用以下命令

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

Referrence: How to get a Docker container's IP address from the host

参考:如何从主机获取Docker容器的IP地址