Search code examples
postgresqlmacosdockerdocker-composepgadmin-4

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:
      - 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


Solution

  • 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.