Home > Enterprise >  Spring Boot with Postgres application not working on Docker. Error: PSQLException: Connection to 127
Spring Boot with Postgres application not working on Docker. Error: PSQLException: Connection to 127

Time:04-13

When trying to build and run my containers using Docker 'docker-compose up' command I keep getting this error:

org.postgresql.util.PSQLException: Connection to 127.0.0.1:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

I have googled for solutions and tried different things but nothing works. My docker-compose.yaml file:

My docker-compose.yaml file:

version: '3.8'
services:
app:
container_name: docker-meetings
image: docker_meetings
build: ./
ports:
- "8080:8080"
expose:
- "8080"
depends_on:
- postgresqldb
postgresqldb:
image: postgres
ports:
- "5432:5432"
expose:
- "5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=db
- POSTGRES_DB=meetingsdb

My Dockerfile:

FROM openjdk:11
ADD target/visma-meeting-app-withdb-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]

My application.properties file:

spring.datasource.url=jdbc:postgresql://postgresqldb:5432/meetingsdb?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&stringtype=unspecified
spring.datasource.username=postgres
spring.datasource.password=db

Maybe anyone has run into this problem before and knows the solution?

CodePudding user response:

Try connecting to localhost port 5432.

If the issue persists then check if PostgreSQL is listening to TCP/IP connections.

" sudo lsof -n -u postgres |grep LISTEN " will show the TCP/IP addresses and ports PostgreSQL is listening on.

CodePudding user response:

I don't have much knowledge about how docker works, but since I'm using postgres in spring I can say that in application.properties files for url part I use

spring.datasource.url=jdbc:postgresql://localhost:5432/dbName

instead of yours

spring.datasource.url=jdbc:postgresql://postgresqldb:5432/meetingsdb?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&stringtype=unspecified

So, I would suggest you can try the same and see if it works. Thanks.

  • Related