Richard
Richard

Reputation: 8935

SpringBoot: Cannot load driver class: org.postgresql.Driver

I have just created a new SpringBoot application (Java11) and am trying to connect to a Postgres database.

When I start the SpringBoot application, there is an error trying to connect to the database. The error reports that it Cannot load driver class: org.postgresql.Driver.

Question

How do I change my configuration below to get the SpringBoot application to connect to the database?

database version

PostgreSQL 12.6 on x86_64-apple-darwin16.7.0, compiled by Apple LLVM version 8.1.0 (clang-802.0.42), 64-bit

pom.xml

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
    </dependency>

application.properties

# pims datasource
spring.datasource.url=jdbc:postgresql://localhost:5432/pims
spring.datasource.username=postgres
spring.datasource.password=
spring.datasource.driverClassName=org.postgresql.Driver
#spring.datasource.driver-class-name=org.postgresql.Driver
#spring.jpa.database-platform=postgres

spring.jpa.show-sql=true
spring.datasource.dbcp2.test-while-idle=true
spring.datasource.dbcp2.validation-query=select 1

#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy

When I run the @SpringBootApplication class, SpringBoot starts to start up as expected, but gets the following error.

error

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2021-06-15 11:58:51.770 ERROR 68967 --- [ main] o.s.boot.SpringApplication
: Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: org.postgresql.Driver

If I remove the following entry (i.e. don't define the driver in application.properties and just have the pom dependency):

spring.datasource.driverClassName=org.postgresql.Driver

I then get the following error:

Failed to load driver class org.postgresql.Driver in either of HikariConfig class loader or Thread context classloader

Upvotes: 3

Views: 16248

Answers (3)

Alexander Khlystov
Alexander Khlystov

Reputation: 91

I have had the same issue. I have looked at IntellIJ > File > Project Structure > Project Settings > Modules > My Module > Dependencies and figured out there was no org.postgresql:postgresql:42.2.23.

So I have Right Clicked on pom.xml and said > Maven > Reload Project.

It worked after that

Upvotes: 5

Orkungdk
Orkungdk

Reputation: 1

You do not need any specific configuration to connect database unless your repository and entity packages are not pulled from another dependency.

I would suggest you to check your connection between your computer and maven repository. You should be missing your postgre dependecy somehow.

(You also would try providing specific version of postgresql artifact)

Upvotes: -2

Richard
Richard

Reputation: 8935

When I expend the external dependency tree, I didn't find any jar for postgressql. After reimport maven dependency, it is added correctly and problem is gone. It is fine now.

Upvotes: 4

Related Questions