I am currently setting up postgreSQL db for my spring-boot project. However, when I try to launch the tomcat server I get exception saying that the repository beans could not be created due to this:
Caused by: jakarta.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution [FATAL: password authentication failed for user "dev-user"] [n/a]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:421) ~[spring-orm-6.1.11.jar:6.1.11]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-6.1.11.jar:6.1.11]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:366) ~[spring-orm-6.1.11.jar:6.1.11]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853) ~[spring-beans-6.1.11.jar:6.1.11]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1802) ~[spring-beans-6.1.11.jar:6.1.11]
... 101 common frames omitted
In my properties.yml I have this:
spring:
datasource:
url: jdbc:postgresql://localhost:5432/authentication_db
username: dev-user
password: password
driver-class-name: org.postgresql.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: false
properties:
hibernate:
format_sql: true
database: postgresql
database-platform: org.hibernate.dialect.PostgreSQLDialect
In my docker-compose.yml I have this:
services:
postgres:
container_name: postgres-sql-pa
image: postgres
environment:
POSTGRES_USER: dev-user
POSTGRES_PASSWORD: password
PGDATA: /var/lib/postgresql/data
POSTGRES_DB: authentication_db
volumes:
- postgres:/data/postgres
ports:
- 5432:5432
networks:
- spring-demo
restart: unless-stopped
Why am I having this exception? I can see that docker container is running fine and the info on the db tells me that the properties db_name, username, password are set correctly
The fix that worked for me was based on @JAsgarov comment. Basicalle changing from 5432:5432 to 5433:5432 and updating the URL