docker compose error "Connection to localhost:5432 refused."

When I run my docker-compose, I face this error: Error message

app         | 2021-09-23 11:52:51.860 ERROR 1 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.
app         |
app         | org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
app         |   at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:280) ~[postgresql-42.2.5.jar!/:42.2.5]
app         |   at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.5.jar!/:42.2.5]
app         |   at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195) ~[postgresql-42.2.5.jar!/:42.2.5]

I tried to change the url in my application.properties file and the error is the same. This is my application.properties file: application.properties

spring.datasource.url=jdbc:postgresql://postgres:5432/employees
spring.datasource.username=postgres
spring.datasource.password=root
spring.jpa.show-sql=true
server.port=8086
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update

` This is my Dockerfile: Dockerfile

from openjdk:8
copy ./target/springboot2-postgresql-jpa-hibernate-crud-example-0.0.1-SNAPSHOT.jar employee-jdbc-0.0.1-SNAPSHOT.jar
CMD ["java","-jar","employee-jdbc-0.0.1-SNAPSHOT.jar"]

And this is my docker-compose: docker-compose

version: "3"
services:
 postgres:
    image: postgres:latest
    network_mode: bridge
    container_name: postgres
    volumes:
      - postgres-data:/var/lib/postgresql/data
    expose:
      - 5430
    ports:
      - 5430:5430
    environment:
      - POSTGRES_PASSWORD=root
      - POSTGRES_USER=postgres
      - POSTGRES_DB=employees

# APP*****************************************
 app:
    image: app
    network_mode: bridge
    container_name: app
    expose:
      - 8081
    ports:
      - 8081:8081
    depends_on:
      - postgres
    links:
      - postgres
    environment:
      - POSTGRES_PASSWORD=root
      - POSTGRES_USER=postgres
      - POSTGRES_DB=employees
      - POSTGRES_URL=jdbc:postgresql://postgres:5432/employees


volumes:
  postgres-data:

1 answer

  • answered 2021-09-23 12:02 Tinku Saini

    Can you run this command to check

    sudo lsof -n -u postgres |grep LISTEN or sudo netstat -ltnp | grep postgres

    should show the TCP/IP addresses and ports PostgreSQL is listening on

    It can be postgres is already running and your image is not running using docker. Maybe killing the process will help you if it is already running.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum