skip to Main Content

I connect can to my pgadmin with db1 but impossible to connect in with db2. I got this error Unable to conect to server: ... (see picture). I have seen some post but none of them resolve my problem.enter image description here

version: "3.9"
services:
  web:
    build:
      context: .
      dockerfile: ./Dockerfile
    entrypoint: /code/docker-entrypoint.sh
    restart: unless-stopped
    ports:
      - "8000:8000"
    depends_on:
      - db1
      - db2
    volumes:
      - .:/code
  db1:
    container_name: database1
    image: postgres:14.4
    restart: unless-stopped
    ports:
      - "5432:5432"
    environment:
      POSTGRES_DB: postgres
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
  db2:
    container_name: database2
    image: postgres:14.4
    restart: unless-stopped
    ports:
      - "5433:5433"
    environment:
      POSTGRES_DB: postgres
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
  pgadmin:
    container_name: pgadmin
    image: dpage/pgadmin4:6.20
    restart: unless-stopped
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: admin
      PGADMIN_CONFIG_SERVER_MODE: 'False'
    volumes:
      - ./pgadmin:/var/lib/pgadmin
    ports:
      - '8001:80'
    depends_on:
      - db1
      - db2
      - web

2

Answers


  1. The issue with db2 is the port

    db2:
        container_name: database2
        image: postgres:14.4
        restart: unless-stopped
        ports:
          - "5433:5433" - this port should map default postgres db : 5432
        environment:
          POSTGRES_DB: postgres
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
    

    change to

    db2:
        container_name: database2
        image: postgres:14.4
        restart: unless-stopped
        ports:
          - "5433:5432"
        environment:
          POSTGRES_DB: postgres
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
    
    Login or Signup to reply.
  2. As PGAdmin is running within the docker network, all you need to do is change the port to 5432 in PGAdmin dashboard. As you (PGAdmin) are already in the same network (docker network) with DB2, docker can resolve the host (db2) and port directly, hence no need to use the port map:

    enter image description here

    But if you want to expose the DB2 to outside of docker network, then you can change the port number in compose (but it is not needed for PGAdmin):

    db2:
        ...
        ports:
          - "5433:5432" #<-- 5432 is the port of the container, 5433 is port of the host ie localhost:5433
        ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search