Where did this come from: database.driverClassName=org.postgresql.Driver
? Don't you mean spring.datasource.driverClassName
?
Spring boot fails to load DataSource using PostgreSQL driver
-
09-07-2023 - |
Question
I have successfully developed a prototype using Spring Boot 1.0.2.RELEASE (was 1.0.1.RELEASE until today).
I have searched and searched and tried solutions like: Spring Boot jdbc datasource autoconfiguration fails on standalone tomcat Spring Boot / Spring Data import.sql doesn't run Spring-Boot-1.0.0.RC1
They all suggests to let Spring Boot do the job. When using H2, everything works, but when I try to switch to PostgreSQL, i get:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.entityManagerFactory(org.springframework.orm.jpa.JpaVendorAdapter)] threw exception; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined
My build.gradle is as follow:
loadConfiguration()
def loadConfiguration() {
def environment = hasProperty('env') ? env : 'dev'
project.ext.envrionment = environment
println "Environment is set to $environment"
def configFile = file('config.groovy')
def config = new ConfigSlurper("$environment").parse(configFile.toURL())
project.ext.config = config
}
buildscript {
ext {
springBootVersion = '1.0.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle- plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
apply plugin: 'groovy'
war {
baseName = 'test'
version = '0.0.1-SNAPSHOT'
}
configurations {
providedRuntime
}
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
compile("org.springframework.boot:spring-boot-starter-thymeleaf:${springBootVersion}")
compile("org.springframework.boot:spring-boot-starter-jdbc:${springBootVersion}")
compile("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}")
compile("postgresql:postgresql:9.1-901.jdbc4")
//compile("com.h2database:h2")
testCompile("org.springframework.boot:spring-boot-starter-test:${springBootVersion}")
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
application.properties:
spring.jpa.database=POSTGRESQL
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=update
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost/cms
spring.datasource.username=cms
spring.datasource.password=NA
Removing application.properties and changing the dependency back to H2 and everything is OK.
I can't find where I am doing wrong :-(
Solution
OTHER TIPS
spring.datasource.driver-class-name=org.postgresql.Driver
Make sure to reload your project if you recently just added postgres to your dependencies.
some time try re-import your mvn project if dependencies are not refreshed , also add following to your pom.xml
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
Or to your build.gradle
implementation "postgresql:postgresql:9.1-901.jdbc4"
I had a similar issue, that required me to check my pom.xml and I had left the H2 dep in, replacing it with
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
Seems to have fixed it for me.
I have this in my build.gradle
:
implementation 'org.postgresql:postgresql:version-number'
but, no matter what i did, the jar was never downloaded.
As a work-around, i downloaded the jar from here(Clicked on Jar(...)
), and added it in my Java Build Path - Libraries manually.
Not ideal, but that's the only thing that works in 2 minutes!
I just came over this thread but it doesent helped me, for me the problem was also in a false configured pom.xml, I just had to add JPA:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Hope that Helps!
I have encountered this issue when our project migrated to Java 17 and gradle 7.5
below steps I have followed to resolve the issue.
gradle import upgraded to : implementation 'org.postgresql:postgresql:42.2.27'
then clean build the project: ./gradlew clean build
then ./gradlew bootRun
solved the issue.
For anyone landing here while looking for plain JDBC solution:
Use jdbc:postgresql://localhost:5432/myDatabase?currentSchema=mySchema
The following works when using Hibernate as JPA provider:
spring.jpa.properties.hibernate.default_schema=mySchema