Question

Deploy Spring boot Application with Postgresql (pgadmin4) and Managed DB

Hello,

I am connecting Postgresql Managed Database(From DO) with pgadmin4(local Postgresql) so that I can deploy spring boot application on Ubuntu server(Droplet). The local database created on pgadmin4 also reflects on Droplet with tables and data in it. I am getting errors related to Database when I run spring boot application.

java -jar App_Service-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod

The spring boot application is running without any errors on local machine but has errors on Ubuntu machine(droplet).

Attaching files related to it:

  • **From Managed Database of Digital Ocean **
username = doadmin
password = ************************ show
host = db-******-**********************.ondigitalocean.com
port = **** 
database = MyDB
sslmode = require
  • Application.yml
server:
  port: 1234
  servlet:
    context-path: /lms
spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/MYDB
    username: postgres   #changed as per Local DB
    password: ****  #changed as per Local DB
    
    
  spring.jpa:
  database: POSTGRESQL
  hibernate.ddl-auto: create-drop
  show-sql: true
    spring.jpa.hibernate.ddl-auto = update
    
  jackson.deserialization.fail-on-unknown-properties: true
  jackson.serialization.fail-on-empty-beans: false  
  mvc.hiddenmethod.filter.enabled: true
  main.allow-bean-definition-overriding: true
  #hibernate ddl Auto for CURDOperations
  jpa.hibernate.ddl-auto: none
        
  spring:
  profiles:
    active: dev
   
---
 spring:
  profiles: dev
  
  server:
  port: ****
  servlet:
    context-path: /lms
  spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/MYDB
    username: postgres   #changed as per Local DB
    password: ****   #changed as per Local DB
    
    spring.jpa:
  database: POSTGRESQL
  hibernate.ddl-auto: create-drop
  show-sql: true
    spring.jpa.hibernate.ddl-auto = update
    
  jackson.deserialization.fail-on-unknown-properties: true
  jackson.serialization.fail-on-empty-beans: false  # 
  mvc.hiddenmethod.filter.enabled: true
  main.allow-bean-definition-overriding: true
  #hibernate ddl Auto for CURDOperations
  jpa.hibernate.ddl-auto: none
      
---
 spring:
  profiles: prod
  server:
  port: **** #port number from DO
  servlet:
    context-path: /lms
    database-platform : org.hibernate.dialect.PostgreSQLDialect
    spring.jpa.hibernate.ddl-auto : create
    
    spring:
  jpa:
    database: POSTGRESQL
    show-sql: true
    hibernate:
      ddl-auto: create-drop
    datasource:
    platform: postgres
    url: jdbc:postgresql://db-***********************.ondigitalocean.com:****/MYDB #digital ocean host with port number
    username: doadmin #digital ocean username
    password:******************** #digital ocean password
    driverClassName: org.postgresql.Driver
  
  jackson.deserialization.fail-on-unknown-properties: true
  jackson.serialization.fail-on-empty-beans: false  # 
  mvc.hiddenmethod.filter.enabled: true
  main.allow-bean-definition-overriding: true
  #hibernate ddl Auto for CURDOperations
  • **Errors on Droplet Machine **
2022-12-13 15:43:24.646  WARN 149006 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 28P01
2022-12-13 15:43:24.648 ERROR 149006 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : FATAL: password authentication failed for user "postgres"
2022-12-13 15:43:24.663 ERROR 149006 --- [           main] j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA EntityManagerFactory: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution
2022-12-13 15:43:24.665  WARN 149006 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution
2022-12-13 15:43:24.672  INFO 149006 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-12-13 15:43:24.729  INFO 149006 --- [           main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-12-13 15:43:24.793 ERROR 149006 --- [           main] o.s.boot.SpringApplication               : Application run failed


Submit an answer
Answer a question...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
December 15, 2022

Hi there,

According to your error here:

FATAL: password authentication failed for user "postgres"

The problem is that your app is trying to use postgres as the database username.

You need to make sure that you are using the prod Spring profile which contains the correct database details, you could try with -Dspring.profiles.active=prod:

https://www.baeldung.com/spring-profiles

Let me know how it goes!

Best,

Bobby