0

I'm attempting to connect PGAdmin to a docker container and found this post (https://stackoverflow.com/a/57729412/11923025) very helpful in doing so. But I've tried testing using a port other than 5432 and am not having any luck.

For example, I tried using 5434 in my docker-compose file, and tried using that port in pgadmin but got the below error (This is the IP address found from using docker inspect)

pgadmin error

This is what my docker-compose file looks like (I am using different ports for 'expose' and 'ports' on purpose, to try and narrow down which one will allow me to connect through PGAdmin, but am having no luck

database:
    image: postgres:10.4-alpine
    container_name: kafka-nodejs-example-database
    environment:
      POSTGRES_USER: "abcdef"
      POSTGRES_PASSWORD: "abcdef"
    expose:
      - "5435"
    ports:
      - 8000:5434
pgadmin:
    image: dpage/pgadmin4
    ports:
      - 5454:5454/tcp
    environment:
      - PGADMIN_DEFAULT_EMAIL=admin@mydomain.com
      - PGADMIN_DEFAULT_PASSWORD=postgres
      - PGADMIN_LISTEN_PORT=5454

Why is is that pgadmin has no issues with 5432 but when I ask it to use another port it throws this error?

I should note, the error in the screenshot above is from attempting to connect postgres container to a pgadmin container. I also tried connecting to the postgres container in my local application of pgadmin, and get a different timeout error below. I even get this same error for port 5432 when trying to connect using my local copy of pgadmin.

pgadmin app error

phenderbender
  • 625
  • 2
  • 8
  • 18

2 Answers2

1

You exposed port 5434 of your container, but PostgreSQL itself is still configured to listen on port 5432. That is why you don't reach the database.

After running initdb and before starting PostgreSQL, configure the cluster, for example with

echo 'port = 5434' >> datadir/postgresql.auto.conf

But there should not be any need to start PostgreSQL on a different port. Just map port 5432 to 5434.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
  • Thank you for the reply - so I have a clarifying question in that case. If I wanted to do this for my dual container approach (both postgres AND pgadmin using containers), I'd need to create a volume and configure that container's auto.conf file? And if I wanted to connect to a postgres container using my **local** pgadmin app, I'd update the auto.conf file for that instance of pgadmin? – phenderbender Jun 17 '20 at 12:14
  • 1
    Sorry if I was unclear: this is about the database, not pgAdmin. – Laurenz Albe Jun 17 '20 at 12:21
  • not your fault, I'm very new to all this but am still having trouble understanding. Since I'm using a postgres image for the container, I'm not running 'initdb' myself at any point. Does that make it not really possible for the container approach to use a port other than 5432? – phenderbender Jun 17 '20 at 12:25
  • 1
    If you have no control over when the server is initialized and started, and you cannot add any actions, then probably you are stuck with 5432. – Laurenz Albe Jun 17 '20 at 12:54
1

The PostgreSQL server listens on port 5432. Just changing things in Docker-level configuration doesn't change where the server process itself listens. That means:

  • The second number in ports: must be 5432. (The first number can be any number you want.)
  • Connections from other Docker containers must connect to port 5432. (ports: are ignored and aren't required.)
  • If you expose: a port, it must also be 5432. (This has no practical effect and duplicates EXPOSE 5432 in the Dockerfile.)

Since each container runs in an isolated network namespace, there's no particular reason to want to change this. You can run multiple database containers, and each will have its own separate container port 5432; they will not conflict.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • When I am creating the server connection in pgadmin as shown in my screenshots above, aren't I telling the server to listen on port 5434, rather than 5432? Sorry if that's a dumb question. It sounds like i should pretty much always be using 5432 then, when using any type of postgres container/connection, and not try to fiddle with it? – phenderbender Jun 17 '20 at 13:05
  • 1
    You're not configuring anything inside the container at all. (Which is why [@LaurenzAlbe's answer](https://stackoverflow.com/a/62428615/10008173) involves changing the PostgreSQL config file.) You need to separately set the port number for the process inside the container, and the port number Docker (and other things) use for connections; these aren't automatically synchronized. – David Maze Jun 17 '20 at 13:19
  • doesn't the "ports:8000:5434" line in the compose file designate how an outside resource can link up with the container and share a connection though? Maybe I'm completely misunderstanding the concepts of ports. Can you help me understand what this line in the compose file accomplishes? I guess I have to both update the config file AND use this line in my compose file? – phenderbender Jun 17 '20 at 13:30
  • 1
    `ports: ['8000:5434']` forwards port 8000 on the host to port 5434 in the container. That doesn't tell the container process anything, though; so the PostgreSQL server is still listening on port 5432, and the port forward fails to connect. – David Maze Jun 17 '20 at 14:11
  • thanks this is starting to make more sense. Can you help me understand the 'listening' aspect of the server a bit more? Is the postgres container the server, or is pgadmin the server in this case? Are you essentially saying that the postgres container is still only allowing information to leave the container via port 5432, and so using 5434 is basically telling the host to look at an empty location? – phenderbender Jun 17 '20 at 14:47