Unable to connect to database (postgres) with SpringBoot (docker) on render.com

46 Views Asked by At

I'm deploying a SpringBoot API on render.com with a Postgres connexion host in render.com too.

So i created a WebService with Docker with this Dockerfile :

FROM maven:3.8.3-openjdk-17 AS build
COPY . .
RUN mvn clean package -DskipTests

FROM openjdk:17.0.1-jdk-slim
COPY --from=build /target/app-0.0.1-SNAPSHOT.jar /app.jar
EXPOSE 8080
EXPOSE 443
ENTRYPOINT ["java", "-jar", "app.jar"]

My Springboot version is 3.1.2 and JDK is 17.

In my application.properties i have :

java.runtime.version=17
server.tomcat.accesslog.enabled=true  

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.datasource.username=app_user
spring.datasource.password=**********
spring.datasource.url=jdbc:postgresql://dpg-cns65eud3nmc739baepg-a.frankfurt-postgres.render.com/appbackend

using Internal Database URL.

When i use External Database URL on my local springboot, that's work, and it's well connected to the render.com's database.

When i use Internal Database URL on my Docker on render.com, build is good, but when it launch my container, i have database connection errors :

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'applicationConfig' defined in URL [jar:file:/app.jar!/BOOT-INF/classes!/com/test/app/security/config/ApplicationConfig.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'userRepository' defined in com.test.app.model.repository.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaSharedEM_entityManagerFactory' while setting bean property 'entityManager'
[...]

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository' defined in com.test.app.model.repository.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaSharedEM_entityManagerFactory' while setting bean property 'entityManager'
[...]  

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaSharedEM_entityManagerFactory': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument
[...]    

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution [Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.] [n/a]
[...]

Caused by: jakarta.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution [Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.] [n/a]
[...]

Caused by: org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution [Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.] [n/a]
[...]

Caused by: 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.
[...]

Caused by: java.net.ConnectException: Connection refused
[...]

I see "localhost" but not used "localhost" .. I've try to put Internal Database URL and External Database URL, same problem.

EDIT : I'd like to add an info, my database and my web service is on the same region : Frankfurt.

Does anyone has this error ?

Thanks a lot for your help, if need more infos, i hope i can help !

0

There are 0 best solutions below