phenderbender
phenderbender

Reputation: 785

Can only connect pgadmin to postgres docker container on port 5432, but not any other port?

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:
      - [email protected]
      - 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

Upvotes: 0

Views: 2048

Answers (2)

Laurenz Albe
Laurenz Albe

Reputation: 247400

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.

Upvotes: 1

David Maze
David Maze

Reputation: 159505

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.

Upvotes: 1

Related Questions