Unable to connect to server: connection failed: connection refused is the server running that host Postgres with Docker

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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

New contributor

Nils W is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật