skip to Main Content

I’m currently using Docker Desktop and I’m encountering an issue with my database. I have been able to use the pgAdmin container without any problems. However, when I attempt to connect the server to the database, I get an error.

Unfortunately, I haven’t been able to determine the cause of this issue. I would appreciate any assistance or suggestions on how to resolve this problem. Thank you in advance for your help.

enter image description here

Here is my docker-compose.yml file:

version: '3'

services:
  myDB:
    image: postgres:16
    container_name: my-database
    restart: always
    ports:
      - 5433:5432
    environment:
      - POSTGRES_USER=alumno
      - POSTGRES_PASSWORD=123456
      - POSTGRES_DB=course-db
    volumes:
      - ./postgres:/var/lib/postgresql/data
  
  pdAdmin:
    image: dpage/pgadmin4
    container_name: pgadmin4
    restart: always
    depends_on:
      - myDB
    ports:
      - 8080:80
    user: '$UID:$GID'
    environment:
      - [email protected]
      - PGADMIN_DEFAULT_PASSWORD=123456
    volumes:
      - ./pgadmin:/var/lib/pgadmin
      - ./pgadmin:/certs/server.cert
      - ./pgadmin:/certs/server.key
      - ./pgadmin:/pgadmin4/servers.json

I need to use port 5433 for this connection, as port 5432 is already being used by PostgreSQL on my PC.

Here is the logs of the container:

2024-05-18 12:17:09 
2024-05-18 12:17:09 PostgreSQL Database directory appears to contain a database; Skipping initialization
2024-05-18 12:17:09 
2024-05-18 12:17:09 2024-05-18 16:17:09.672 UTC [1] LOG:  starting PostgreSQL 16.3 (Debian 16.3-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
2024-05-18 12:17:09 2024-05-18 16:17:09.672 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2024-05-18 12:17:09 2024-05-18 16:17:09.672 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2024-05-18 12:17:09 2024-05-18 16:17:09.685 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2024-05-18 12:17:09 2024-05-18 16:17:09.698 UTC [29] LOG:  database system was shut down at 2024-05-18 16:16:54 UTC
2024-05-18 12:17:09 2024-05-18 16:17:09.709 UTC [1] LOG:  database system is ready to accept connections
2024-05-18 12:22:09 2024-05-18 16:22:09.750 UTC [27] LOG:  checkpoint starting: time
2024-05-18 12:22:09 2024-05-18 16:22:09.804 UTC [27] LOG:  checkpoint complete: wrote 3 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.015 s, sync=0.007 s, total=0.055 s; sync files=2, longest=0.004 s, average=0.004 s; distance=0 kB, estimate=0 kB; lsn=0/19536E0, redo lsn=0/19536A8
2024-05-18 12:43:36 2024-05-18 16:43:36.351 UTC [91] LOG:  invalid length of startup packet
2024-05-18 12:43:37 2024-05-18 16:43:37.483 UTC [92] LOG:  invalid length of startup packet
2024-05-18 12:43:38 2024-05-18 16:43:38.965 UTC [93] LOG:  invalid length of startup packet
2024-05-18 12:43:39 2024-05-18 16:43:39.974 UTC [94] LOG:  invalid length of startup packet

2

Answers


  1. It seems like you try to connect from one container in your cluster to another.
    What you’d propably want to do is have them in the same network since you’re using a docker-compose.yml file.
    To do this, you can add a network in your docker-compose.yml file and add both of your services to that network like this:

    services:
      myDB:
        image: postgres:16
        container_name: my-database
        restart: always
        ports:
          - 5433:5432
        environment:
          - POSTGRES_USER=alumno
          - POSTGRES_PASSWORD=123456
          - POSTGRES_DB=course-db
        volumes:
          - ./postgres:/var/lib/postgresql/data
        networks:
          - my-network
      pdAdmin:
        image: dpage/pgadmin4
        container_name: pgadmin4
        restart: always
        depends_on:
          - myDB
        ports:
          - 8080:80
        user: '$UID:$GID'
        environment:
          - [email protected]
          - PGADMIN_DEFAULT_PASSWORD=123456
        volumes:
          - ./pgadmin:/var/lib/pgadmin
          - ./pgadmin:/certs/server.cert
          - ./pgadmin:/certs/server.key
          - ./pgadmin:/pgadmin4/servers.json
        networks:
          - my-network
    networks:
      my-network:
        driver: bridge
    

    Docker will then automatically take care of networking and you will be able to connect to your postgres container from inside your pgadmin-container using the container name. Take note that you only need to specify the service.ports config when you want to connect from your host system using localhost:port. This means that you’ll have to connect via port 5432 (the port your container is listening on) instead of 5433 when connecting from inside the network

    Login or Signup to reply.
  2. You need to use myDB as Connection - Host name/Adress value when adding new server at PG Admin. This is because you defined postgres container with myDB service name at your compose file.

    While connecting, service name is important, not the container name.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search