200

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

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

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.

edi9999
  • 19,701
  • 13
  • 88
  • 127
Rigoxls
  • 2,117
  • 2
  • 13
  • 5
  • 2
    You don't say whether PostgreSQL is listening for external connections (check your config files) or if that port appears open from the local machine. – Richard Huxton Aug 28 '14 at 08:21
  • 3
    It doesn't seem possible to answer that question with so little info provided – cthulhu Aug 28 '14 at 13:47
  • 1
    This goes without saying, but double check that you dont' have a local postgresql instance running. I was banging my head against a wall trying to figure this out. I forgot my mac started a psql instance on startup. I turned it off with this command `brew services stop postgresql`. This fixed it for me. – Jeff Gruenbaum Jun 14 '22 at 16:32

30 Answers30

153

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
    
hs-
  • 176
  • 2
  • 5
  • 21
stack.it
  • 1,541
  • 1
  • 9
  • 3
  • 42
    If you are publishing the port with this `-p 5432:5432` then you don't need to find the container's IP you can use your host IP, or `127.0.0.1` or just `localhost` – Davos Nov 10 '17 at 15:55
  • The intent of the post is valid but it is not a high quality post for SE. To start, there is no actual 'question' posed. Additionally, there are no indications of how they tried the process or errors they received while doing it. That said, I think you've done a fine job of responding. – duct_tape_coder Jun 20 '19 at 14:41
  • 13
    This answer is useful to me, therefore it's a valid question. – Todd Jun 17 '20 at 21:04
  • 2
    I've used -p 5432:5432 to map the container to my host but still getting a connection error. I tried using the IPAddress I got from docker inspect and it works. – ginad Sep 15 '20 at 15:40
  • Thanks, it's step 3 "Using docker's inspect command find the IP" that unblocked the problem I was having. – jpierson Mar 31 '21 at 21:14
  • when I do docker inspect I don't get the IP address. why? – Tausif Sayyad Feb 03 '22 at 06:36
  • When I try accessing it via `psql -h 172.17.0.2 -U postgres` or via pgadmin given that `IPAddress` from `docker inspect` it says could not connect to server. The port shows `0.0.0.0:5432->5432/tcp`. I tried both `172.17.0.2` and `172.17.0.1`. But same, connection failed. Please help – Mon Nov 16 '22 at 22:01
  • 1
    When I run `docker exec -it psql -U postgres` it works, so I think the container runs fine? However I still can't access it using the `IPAddress` nor `Gateway` from `docker inspect`. How did you make it work? I'm using macOS Monterey – Mon Nov 16 '22 at 22:04
151

1. Postgres with Docker

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

2. PgAdmin with Docker

docker run \
  -p 5050:80 \
  -e "PGADMIN_DEFAULT_EMAIL=name@example.com" \
  -e "PGADMIN_DEFAULT_PASSWORD=admin" \
  -d dpage/pgadmin4

3. Connection string for PgAdmin

Enter PgAdmin on localhost:80. Then add a server with:

name: container-postgresdb
host: host.docker.internal
database: postgres
user: postgres
password: admin

You can also use this to find out the host address — it will be listed as IPAddress in the output —:

docker inspect container-postgresdb \
  -f "{{json .NetworkSettings.Networks }}"

This other article might help with more info.

Philippe Fanaro
  • 6,148
  • 6
  • 38
  • 76
Thavaprakash Swaminathan
  • 6,226
  • 2
  • 30
  • 31
  • 18
    `Unable to connect to server: could not translate host name "host.docker.internal" to address: Try again` – duct_tape_coder Jun 20 '19 at 14:23
  • 9
    I also used this approach to create both postgres and pgAdmin in docker. What worked for me as an input value was the answer from @SolidSnake - https://stackoverflow.com/a/58224344/4288961 > docker inspect CONTAINER_ID | grep IPAddress. and used this result IP address as a host – Prusdrum Apr 08 '20 at 11:03
  • 7
    Awesome!. Very helpful. I only needed the 'host.docker.internal' value – Hernán Acosta Oct 21 '20 at 18:30
  • 6
    'host.docker.internal' is exactly what I was missing, thanks!! – Jeremy C. Sep 15 '21 at 19:41
  • 3
    host.docker.internal - that's what I was looking for, thanks – devielu Jun 23 '22 at 11:40
  • 1
    It says `Unable to connect to server: could not translate host name "host.docker.internal" to address: nodename nor servname provided, or not known` :( I also tried using the `IPAddress` and `Gateway` from docker inspect but it also says could not connect to server and some timeout :( when I try to access it using docker exec it's fine, but can't get it to work w/ pgadmin :( – Mon Nov 16 '22 at 22:08
  • I used 80:80 port and it works for me. – Manish Sharma May 08 '23 at 12:16
74

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

  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.
Philipp Kief
  • 8,172
  • 5
  • 33
  • 43
Maosheng Wang
  • 972
  • 6
  • 12
69

Alternatively, you could combine Postgres and Pgadmin in one docker-compose file, and login as user pgadmin4@pgadmin.org, pwd: admin. To add the Posgres server, use 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: pgadmin4@pgadmin.org
      PGADMIN_DEFAULT_PASSWORD: admin
    restart: unless-stopped

volumes:
  postgres-data:
Erik Vullings
  • 5,550
  • 2
  • 28
  • 23
  • 3
    I get 'could not translate host name "postgres" to address: nodename nor servname provided, or not known'. – Enrique Ortiz Casillas Feb 28 '20 at 17:39
  • 1
    could not translate host name "postgres" to address: Name does not resolve – proxy Jun 01 '20 at 16:55
  • 6
    if you `docker network ls`, you'll see a bridge for the postgres-to-pgadmin network (mine is postgres_postgres). Then, you can `docker network inspect postgres_postgres` and see two containers, each with an ip/hostname. Take the one from Postgres (mine is postgres_db_1) to successfully connect from pgAdmin. –  Sep 17 '20 at 10:43
  • @Nil this should be the top-ranked answer! – nazkord Oct 12 '22 at 08:15
68

If you verified that PostgreSQL is running and you can connect there with local copy of PgAdmin...

The answer is simple: use host.docker.internal istead of localhost for the PgAdmin running inside the Docker

use <code>host.docker.internal</code> istead of <code>localhost</code>

KARPOLAN
  • 2,507
  • 1
  • 19
  • 10
38

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

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

SolidSnake
  • 1,399
  • 1
  • 9
  • 11
  • https://stackoverflow.com/questions/17157721/how-to-get-a-docker-containers-ip-address-from-the-host – José Pablo Ceballos May 01 '22 at 11:01
  • This IP address is all but useless. It only works on one very specific Docker setup (a native Linux system without Docker Desktop or a similar VM-based setup, locally from the same machine). The other answers to this question are more portable across environments. – David Maze Aug 06 '23 at 19:02
26

I included this in the docker yaml file to get the database and 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:
      - PGADMIN_DEFAULT_EMAIL=admin@mydomain.com
      - PGADMIN_DEFAULT_PASSWORD=postgres
      - PGADMIN_LISTEN_PORT=5454

The postgres username is alphaone and the password is xxxxxxxxxxx.

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

eg: docker inspect 2f50fabe8a87 | grep IPAddress

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

pgAdmin

Afshin Ghazi
  • 2,784
  • 4
  • 23
  • 37
  • 1
    This helped. I had to grep the correct IP address from within the container associated with docker Postgres to add to the server configuration in docker PgAdmin portal like shown above. Putting `localhost` didn't work. – Andrew Lobban Oct 31 '19 at 20:36
21

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.

1. Run a postgres container:

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

2. Run pgAdmin container:

docker run -p 80:80 --link some-postgres -e "PGADMIN_DEFAULT_EMAIL=email@domain.com" -e "PGADMIN_DEFAULT_PASSWORD=postgres" -d dpage/pgadmin4

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

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

Aleksandr Makov
  • 2,820
  • 3
  • 37
  • 62
  • 1
    I can connect with psq to the server, and I get log into the pgadmin interface. But what network/port do I use when connecting to the postgres server from the pgadmin interface? – Tom Mar 09 '19 at 18:57
  • 1
    Worth noting from the Docker docs (https://docs.docker.com/network/links/) `Warning: The --link flag is a legacy feature of Docker. It may eventually be removed. Unless you absolutely need to continue using it, we recommend that you use user-defined networks to facilitate communication between two containers instead of using --link.` I also did not have any luck with using `--link` to get my pgadmin container to talk to my postgres container. I can access the postgres from a local install of pgadmin however. – duct_tape_coder Jun 20 '19 at 14:43
13

After facing this issue for two days i was able to resolve that issue.

solution of this problem is already answered by peoples like do inspect

docker inspect CONTAINER_ID

but while running this command i got a-lot of logs like Host Config Config Network Settings etc. so i got confused which IPAddress to add in the pgAdmin connection because i tried 0.0.0.0 and config, host, networkSettings different -2 IPAddress in the logs but finally it works after trying a-lot.

it works with which IP, we have to add that network ip address (which we created to connect the postgres and pgAdmin.)

like in my case when i run :-

docker inspect postgres_container

 "NetworkSettings": {
        "Bridge": "",
        "SandboxID": "sdffsffsfsfsfsf123232e2r3pru3ouoyoyuosyvo8769696796",
        "HairpinMode": false,
        "LinkLocalIPv6Address": "",
        "LinkLocalIPv6PrefixLen": 0,
        "Ports": {
            "5432/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "5432"
                }
            ]
        },
        "SandboxKey": "/var/run/docker/231231Ad132",
        "SecondaryIPAddresses": null,
        "SecondaryIPv6Addresses": null,
        "EndpointID": "",
        "Gateway": "",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "IPAddress": "",
        "IPPrefixLen": 0,
        "IPv6Gateway": "",
        "MacAddress": "",
        "Networks": {
            "postgres": {
                "IPAMConfig": null,
                "Links": null,
                "Aliases": [
                    "postgres",
                    "0e2j3bn2m42"
                ],
                "NetworkID": "35rhlhl3l53l5hlh3523233k3k4k22l3hk2k4",
                "EndpointID":"b3424n43k52o2i4n235k1k4l2l4hj234f14n2",
                "Gateway": "192.168.16.1",
                "IPAddress": "192.168.16.2",
                "IPPrefixLen": 20,
                "IPv6Gateway": "",

so we have to add the NetworkSettings -> Network -> Postgres(mine created network) -> IPAddress i.e. "IPAddress": "192.168.16.2".

After adding this ip it will work.

I hope it will help.

Sunny Goel
  • 1,982
  • 2
  • 15
  • 21
13

This worked for me on Ubuntu 18:

1- Run a postgres container

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

2- Run a pgAdmin container

docker run --rm -p 5050:5050 thajeztah/pgadmin4

3- Get your local IP (in Ubuntu by ifconfig command)

4- Open localhost:5050 in your browser

5- Click on Servers >> Create >> Server...

6- In General tab, give it a name, for example: docker. In Connection tab, enter these fields:

Host name: The IP from 3

Username: user

Password: password123

7- Click on Save and now everything should work fine.

Note: If this didn't work, try "localhost" for the host name.

Amid Gol
  • 305
  • 2
  • 7
  • 1
    STEP 3 was found only here and it finally worked for me! I have pgadmin in a docker only because on arm64 there is no native package. So specifying 192.168.0.61 found by ifconfig and enabling external connections at pg config allowed me to connect to an already running database outside the docker. – user109764 Nov 08 '21 at 22:03
  • Thanks! specifying my local IP has made it work, but i don't understand why "localhost" or "0.0.0.0" didn't work but a 192... IP worked? Can someone explain to me? – Doğuş Oct 19 '22 at 13:19
8

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

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

Haroon Khalid
  • 155
  • 2
  • 8
7

If you're on a mac and localhost use below as credentials:

version: '3.5'

services:
  postgres:
    container_name: postgres_container
    image: postgres
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-changeme}
      PGDATA: /data/postgres
    volumes:
       - postgres:/data/postgres
    ports:
      - "5432:5432"
    networks:
      - postgres
    restart: unless-stopped
  
  pgadmin:
    container_name: pgadmin_container
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4@pgadmin.org}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
      PGADMIN_CONFIG_SERVER_MODE: 'False'
    volumes:
       - pgadmin:/var/lib/pgadmin

    ports:
      - "${PGADMIN_PORT:-5050}:80"
    networks:
      - postgres
    restart: unless-stopped

networks:
  postgres:
    driver: bridge

volumes:
    postgres:
    pgadmin:

Connection:

Hostname/Address: host.docker.internal or 0.0.0.0

Port: 5432

username: postgres

database: postgres

password: changeme

Payam Khaninejad
  • 7,692
  • 6
  • 45
  • 55
5

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

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.

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

greemwahr
  • 167
  • 2
  • 10
5

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:

  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 --network command.

    docker run -p 85:80 --network docker_default -e 'PGADMIN_DEFAULT_EMAIL=user@domain.com' -e 'PGADMIN_DEFAULT_PASSWORD=SuperSecret' -d dpage/pgadmin4

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

I hope this can be useful for someone. Regards.

hizmarck
  • 686
  • 9
  • 19
5

When you start container you have network alias for it. Just use this alias in pgadmin UI. Like if you have the following docker compose config:

version: "3.5"
services:
  postgres:
    container_name: postgres-14
    image: postgres:14.1
    restart: unless-stopped
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_HOST_AUTH_METHOD=trust
      - POSTGRES_PASSWORD=root
      - POSTGRES_USER=postgres
    hostname: postgres
  pgadmin:
    container_name: pgadmin4
    image: dpage/pgadmin4
    restart: unless-stopped
    ports:
      - "80:80"
    environment:
      - POSTGRES_HOST_AUTH_METHOD=trust
      - PGADMIN_DEFAULT_EMAIL=user@domain.com
      - PGADMIN_DEFAULT_PASSWORD=admin
    hostname: pgadmin

You can add server with hostname: postgres

enter image description here

Sergei Dubinin
  • 306
  • 3
  • 15
4

You can create a Docker bridge network to do this too.

$ docker network create pgnet
a0ae44aaf6f806fc37302e4c603b4828c4edb8d487fd9cd90e2fb19ae1d5c65f

$ docker run --detach \
    --name pg \
    --network pgnet \
    --publish 5432:5432 \
    --restart always \
    --env POSTGRES_PASSWORD=pg123 \
    --volume ${PWD}/data:/var/lib/postgresql/data \
    postgres:12.1
b60611e43727dabe483c1f08fdf74961b886ce05b432a10a0625bd1b6db87899

$ docker run --detach \
    --name pgadm \
    --network pgnet \
    --publish 8000:80 \
    --volume ${PWD}/pgadmin:/var/lib/pgadmin \
    --env PGADMIN_DEFAULT_EMAIL=user@domain.com \
    --env PGADMIN_DEFAULT_PASSWORD=pgadm123 \
    dpage/pgadmin4:4.20
27f9ce1c1c4c383ee1507f4e2d88f6ef33d4fcf5b209a8a03b87519f90d56312

Open http://localhost:8000/

  1. Click Add New Server
  2. Create - Server
    1. Name: db
    2. Hostname/address: pg
    3. Username: postgres
    4. Password: pg123
  3. Save

The Hostname/address used above is the name of the Postgres container given by --name pg

Everett Toews
  • 10,337
  • 10
  • 44
  • 45
  • This is a unique approach, using the container name as the hostname. Same approach as setting up Nginx or something. Makes sense. – Sean McCarthy Nov 08 '20 at 00:05
4

Here are the full steps that worked for me

  1. Download and install pgadmin GUI tool

  2. Run docker container: docker-compose up my-db.

From this command logs, note down ipv4 address and port. In my case, it's 0.0.0.0 and 5432.

enter image description here

  1. Open docker-compose.yml file and note down POSTGRES PASSWORD

enter image description here

  1. open pgadmin gui
  • enter name

enter image description here

In connection tab:

  • enter that ipv4 address in Hostname/address
  • enter that port
  • enter that POSTGRES PASSWORD in password
  • save.

enter image description here

GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
3

Are you using Window Subsystem LinuxWSL2 to run Docker and PgAdmin? The steps I suggested is similar to what folks suggested. In my case I am using window environment

Step 1: Open CMD and type ipconfig and hit enter.

Step 2: Check WSL IPv4 Adress

Ethernet adapter vEthernet (WSL):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : 3bd9::997b:b52a::fe80::65b3%63
   IPv4 Address. . . . . . . . . . . : 172.172.172.1  // use the IP as host/address
   Subnet Mask . . . . . . . . . . . : 255.255.255.255

Step 3: Open PgAdmin from the browse and create a server

Step 4:

// Here it depends on your desired config mine is the following

Server: postgres
host address:  IPv4 Address(step 2)
username: postgress(username for postgress)
password: password(password for postgress)

I spend two days to figure out what was wrong, I hope someone will find it helpful

Niyongabo Eric
  • 1,333
  • 18
  • 21
2

To find the correct ip for your container, execute the following commands:

Check container ID:

docker ps

To check the correct IP of your container:

docker inspect <ID_CONTAINER> | grep "IPAddress"

The configuration of both PostgreSQL and PgAdmin with Docker are simple, if not right, I recommend redoing the configuration from scratch, if your problem is deeper.

Here is a script I developed to solve this problem. script-sh/install-docker-postgres-and-pgadmin

2

For macOS IPs of postgres and pgadmin4 are different from the ones docker inspect provides. Type

docker-machine ls

Take a look at the name of the server on the left. It's default by default.

docker-machine ip default will IP you need to use for both, pgadmin4 in browser and for postgres in pgadmin4 settings.

Andrei Konstantinov
  • 6,971
  • 4
  • 41
  • 57
2

If local host port 5432 already in use by another psql servers, change it when creating the container and in Pgadmin. screen

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
alex-shef
  • 21
  • 2
2

What I have done success on macOS Monterrey running Docker Desktop for macOS(M1):

  1. Pull the latest postgres:

docker pull postgres

  1. Run the postgres container:

docker run -d -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres --name postgres-server -p 5432:5432 --restart=always postgres

  1. Then installed the latest version of pgAdmin 4 (macOS) from pgadmin website

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

pgAdmin 4: Connection postgres-server

Raúl LG
  • 21
  • 2
1

I did not connect with my container pg (dpage/pgadmin4 image) from browser on 0.0.0.0:9090 .

I ran this command :

docker run --name pg -p 9090:9090 -e PGADMIN_DEFAULT_EMAIL='faizan' -e PGADMIN_DEFAULT_PASSWORD='faizan'  -d dpage/pgadmin4

Solution one :

First I tried to inspect the container for IP address

docker inspect pg 

I got the container IP address "IPAddress": "172.17.0.3". http://172.17.0.3:9090 did not accessible.

Solution two :

And then I ran command without de-attached mode (-d removed)

docker run --name pg -p 9090:9090 -e PGADMIN_DEFAULT_EMAIL='faizan' -e PGADMIN_DEFAULT_PASSWORD='faizan' dpage/pgadmin4
# Output, it should be running on 9090.
Listening at: http://[::]:80

-p 9090:9090 did not work.

Finally I found the solution :

On some filesystems that do not support extended attributes, it may not be possible to run pgAdmin without specifying a value for PGADMIN_LISTEN_PORT that is greater than 1024. In such cases, specify an alternate port when launching the container by adding the environment variable, for example:

-e 'PGADMIN_LISTEN_PORT=5050'

Don’t forget to adjust any host-container port mapping accordingly.

Read more about it official doc

I added -e 'PGADMIN_LISTEN_PORT=9090' and worked for me.

You can try both solutions, it will definitely work for you.

1

I ran this, It worked for me

docker pull dpage/pgadmin4
docker run -p 9000:80 -e 'PGADMIN_DEFAULT_EMAIL=test@gmail.com' -e 'PGADMIN_DEFAULT_PASSWORD=root' -d dpage/pgadmin4
Rajitha Bhanuka
  • 714
  • 10
  • 11
1

Go to your container terminal on Docker Desktop and type hostname -I in case is linux to get the ip assigned to this machine, then go to your pgAdmin an try it out. @Rigoxls

Docker Desktop

Juan Sanchez
  • 543
  • 7
  • 14
1

Have running postgres and pgadmin docker containers in on my windows machine using following docker compose :-

services:   postgresql:
    container_name: postgresql
    image: postgres
    environment:
      POSTGRES_USER: ayushhh
      POSTGRES_PASSWORD: ayushhh
      PGDATA: /data/postgres
    volumes:
      - postgres:/data/postgres
    ports:
      - "5432:5432"
    networks:
      - postgres
    restart: unless-stopped   pgadmin:
    container_name: pgadmin
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4@pgadmin.org}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
      PGADMIN_CONFIG_SERVER_MODE: 'False'
    volumes:
      - pgadmin:/var/lib/pgadmin
    ports:
      - "5050:80"
    networks:
      - postgres
    restart: unless-stopped

and have used host => host.docker.internal

enter image description here

Ayush Sharma
  • 69
  • 2
  • 5
0

The solution I tried and worked for me, was to create a docker compose file where I included postgress and pgadmin as services. For more details: Connecting pgadmin to postgres in docker

Andrei Bacescu
  • 569
  • 1
  • 5
  • 11
0

Tested on Mac

pgAdmin-4: version 6.12

I tried several answers here and in other places but nothing worked for me.

Here is the problem I had and the solution. I had a docker postgres running my machine and need to connect this server in pgAdmin.

All I need to do was to check the docker information and fill the server configs based on that in pgAdmin.

You can do this by opening your docker and going to dashboard and then selecting the postgres image you're using. There is a tab called Inspect click on that. Here you can get all the information about that postgres container.

Now open the pgAdmin and resister a server by right clicking on the servers -> Register -> Server.

choose a name for the server and click Connection tab. Here we need to fill the data like host name/address, IP, ports and etc.

In docker Inspect tab, there is Ports section that you can get this data from, something like this:

ports

So your host address based on this picture is 172.0.0.1 and port is 5873. Other info like database, user and password is also available in Environment section in docker Inspect tab.

Now fill the other information click save in pgAdmin and it should work.

NoobN3rd
  • 1,223
  • 1
  • 9
  • 19
0

in my case just try to see the container name by docker ps then just will be  see then docker inspect 311753dcc4bf which 311753dcc4bf id

"IPAddress":

Mohammed Al-Reai
  • 2,344
  • 14
  • 18
0

Not sure how many people this is going to help but I had to stop the currently running database from my windows machine. Once I did it I was able to connect to postgres database running in container

  1. Navigate to "C:\Program Files\PostgreSQL\15\bin" if you can run "pg_ctl" command

  2. Run below command

    .\pg_ctl.exe -D "C:\Program Files\PostgreSQL\15\data" stop

Hope this helps

Ravi Khambhati
  • 637
  • 1
  • 12
  • 35