I'm encountering an issue when trying to run a Spring Boot application along with a PostgreSQL container using Docker Compose. The Spring Boot application is unable to connect to the PostgreSQL container, and I'm consistently getting the following error:
org.postgresql.util.PSQLException: Connection to postgresql:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
I am using two separate Docker Compose files, one for the Spring Boot application and another for the PostgreSQL container and I declared the containers in the same network.
In the Spring Boot application properties, I'm using the correct PostgreSQL container name in the datasource URL:
application.properties file
spring.jpa.hibernate.ddl-auto=update spring.datasource.url=jdbc:postgresql://postgresql:5432/mydb spring.datasource.username=admin spring.datasource.password=admin
database docker-compose file
version: '2' services:
networks: mynetwork: driver: bridge
database container
database: image: postgis/postgis container_name: postgresql networks: - mynetwork environment: - POSTGRES_USER=admin - POSTGRES_PASSWORD=admin - POSTGRES_DB=mydb ports: - "5432:5432" volumes: - "./datadb:/var/lib/postgresql/data"Spring docker-compose file
version: '2' services: backend: build: . container_name: spring-application ports: - 8085:8085 networks: - mynetwork environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://postgresql:5432/mydb - SPRING_DATASOURCE_USERNAME=admin - SPRING_DATASOURCE_PASSWORD=admin - SPRING_JPA_HIBERNATE_DDL_AUTO=update depends on: - databasenetworks: mynetwork: driver: bridge
I also add this line in the pg_hba.conf file and retsart the postgresql container:
host all all 0.0.0.0/0 trust
Despite these configurations, the connection is refused, and the Spring Boot application fails to start.
what I am missing? Any ideas PLEASE
possible solutions to address this issue please?
spring.datasource.urltojdbc:postgresql://localhost:5432/mydband checkdocker-composefiles. Am i right ?