1

I've set up a local lab environment for learning Postgres. I'm using Docker Compose to run a postgres container and a pgAdmin instance next to each other. I can also successfully connect to the database server from pgAdmin's UI.

My only problem is that I can only manage to connect to the DB server using the internal IP of the postgres container as the host in the connection settings and this changes every time I restart my containers, so I have to check the new internal IP and manually update that on the UI of pgAdmin every time I restart my environment.

I've tried connecting using localhost (or 127.0.0.1) and the exposed port but pgAdmin gives an error for that.

connection to server at "localhost" (127.0.0.1), port 6543 failed:
Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (::1), port 6543 failed: Address not available
Is the server running on that host and accepting TCP/IP connections?127.0.0.1

I double check my running containers using docker ps and I see:

25e9bfb30fe6 postgres "docker-entrypoint.s…" 41 minutes ago Up 41 minutes 0.0.0.0:6543->5432/tcp, :::6543->5432/tcp sql-for-beginners_postgres_1

So my postgres container is in fact exposed to the host on port 6543.

Also if I run telnet 127.0.0.1 6543 I get a successful connection. So then why can't I connect to it from pgAdmin using my host's IP?

I've also tried using host.docker.internal as the host in pgAdmin, but that also gives an error:

could not translate host name "host.docker.internal" to address: Name does not resolve

The only way I could connect is via the internal IP, which is not persistent between stoping and staring the containers.

So my question is: Is there a way to set pgAdmin to a connection that will be persistent between container restarts?

My docker-compose.yml looks like this:

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
    volumes:
      - pgadmin-data:/var/lib/pgadmin
    restart: unless-stopped

volumes:
  postgres-data:
  pgadmin-data:

Cooty
  • 365
  • 4
  • 14
  • 2
    Use the other container's Compose service name `postgres` and the standard PostgreSQL port 5432. In Docker each container is its own `localhost`, and connections between containers ignore `ports:`. Also see [Networking in Compose](https://docs.docker.com/compose/networking/) in the Docker documentation. – David Maze Feb 21 '23 at 10:55

0 Answers0