2

Question seems identical to Can not connect to Postgres Container from pgAdmin but none of those solutions seem to be working, and I'm using slightly different tools... so maybe different problem?

In my Docker Desktop (running Windows with a Linux Docker), standard install, I've installed the postgres:latest image and created a container from it. I used the following settings:

Screen Shot: PG Docker Image install

The log in the container shows success:

2023-05-28 14:56:30 2023-05-28 18:56:30.313 UTC [1] LOG:  starting PostgreSQL 15.3 (Debian 15.3-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2023-05-28 14:56:30 2023-05-28 18:56:30.313 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2023-05-28 14:56:30 2023-05-28 18:56:30.313 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2023-05-28 14:56:30 2023-05-28 18:56:30.319 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-05-28 14:56:30 2023-05-28 18:56:30.325 UTC [62] LOG:  database system was shut down at 2023-05-28 18:56:30 UTC
2023-05-28 14:56:30 2023-05-28 18:56:30.329 UTC [1] LOG:  database system is ready to accept connections

On the terminal tab, I'm able to connect to the local instance:

# psql -p 5432 -U postgres
psql (15.3 (Debian 15.3-1.pgdg110+1))
Type "help" for help.

postgres=# \l
                                                List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    | ICU Locale | Locale Provider |   Access privileges   
-----------+----------+----------+------------+------------+------------+-----------------+-----------------------
 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 |            | libc            | 
 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 |            | libc            | =c/postgres          +
           |          |          |            |            |            |                 | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 |            | libc            | =c/postgres          +
           |          |          |            |            |            |                 | postgres=CTc/postgres
(3 rows)

postgres=# 

I did a docker inspect pgserver and saw the network settings as such:

"NetworkSettings": {
            "Bridge": "",
            "SandboxID": "7752ebc73909a6a14039b287472724b48fb8138d4466a41eb368fd91057dbcbc",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "5432/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "54320"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/7752ebc73909",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "a00a861f13fb77a019123b42affeed42f11a8cac8ab00f609b6b2cbfc76e4ee6",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.3",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:03",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "a28aecb0b5fd55718069e079194fcf39c84a380041a82f0ec6de8f7b3565dd6a",
                    "EndpointID": "a00a861f13fb77a019123b42affeed42f11a8cac8ab00f609b6b2cbfc76e4ee6",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:03",
                    "DriverOpts": null
                }
            }
        }

I have also installed the PGAdmin4 Docker Desktop extension, but I cannot configure it to connect to my postgres instance.

  • I have tried to connect to 127.0.0.1 on ports 5432 and 54320.
  • I have tried to connect to 172.17.0.3 on the same ports.
  • I have tried to connect to pgserver on the same ports.
  • I have tried to connect to localhost on the same ports.
  • I have ensured that SSL Mode is set to "prefer".

Depending on what I use, I get an error message that the connection is refused, or a connection timeout error.

What am I doing wrong?

Per request in comments, results of docker ps are below.

docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED        STATUS        PORTS                     NAMES
0ed9f3f293fd   postgres:latest   "docker-entrypoint.s…"   20 hours ago   Up 20 hours   0.0.0.0:54320->5432/tcp   pgserver
2
  • can you provide the screenshot for the following command docker ps Commented May 28, 2023 at 20:31
  • @Balint I've added the data you asked for. Commented May 29, 2023 at 15:30

2 Answers 2

8

Docker is not able to resolve the hostname as PgAdmin extension is also running in a separate container.

Use host.docker.internal instead of localhost or 127.0.0.1 in PgAdmin connection properties to access the PostgreSQL server.

PgAdmin Database Connection Properties

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the hint. Works as expected.
0

I have a feeling that the problem is not docker or the port configuration But the configuration of postgress in particular what connection are allowed defined in the

/var/lib/pgsql/12/data/pg_hba.conf

you may need to configure it like

host    all             all             0.0.0.0/0            md5

you need to do this in way the is persistent via volumes

2 Comments

I added this line and restarted the image (path was a bit different, but not hard to figure out) but I am not seeing any difference in behavior.
will try to replicate on my machine I will be in touch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.