Spring Boot
PostgreSQL
datasource
exception handling
JDBC driver

Spring Boot PostgreSQL DataSource: "Failed to determine a suitable driver class"

Codemia Knowledge Hub ·

When Spring Boot fails to create a PostgreSQL DataSource, the cause is usually one of four things: the JDBC driver is missing, the URL is wrong, the database is unreachable, or a custom configuration has overridden Boot's auto-configuration and broken it. Identify which layer failed instead of treating every DataSource exception as a generic PostgreSQL problem.

Add the PostgreSQL driver dependency

Spring Boot cannot create a PostgreSQL DataSource without the PostgreSQL JDBC driver on the classpath.

For Maven:

xml
1<dependency>
2  <groupId>org.postgresql</groupId>
3  <artifactId>postgresql</artifactId>
4</dependency>

For Gradle:

groovy
dependencies {
    implementation 'org.postgresql:postgresql'
}

A missing driver typically produces Cannot load driver class: org.postgresql.Driver or Failed to determine a suitable driver class.

In modern Spring Boot you usually do not need to set spring.datasource.driver-class-name. Boot infers the driver from the JDBC URL when the dependency is present.

Verify the datasource properties

Next, check the connection configuration. A valid minimal setup:

properties
spring.datasource.url=jdbc:postgresql://localhost:5432/appdb
spring.datasource.username=appuser
spring.datasource.password=secret

Equivalent YAML:

yaml
1spring:
2  datasource:
3    url: jdbc:postgresql://localhost:5432/appdb
4    username: appuser
5    password: secret

Common mistakes:

  • using jdbc:postgres:// instead of jdbc:postgresql://
  • pointing to the wrong host or port
  • misspelling the database name
  • loading the wrong Spring profile, so the expected properties never apply

With a malformed URL, the driver may be present but unable to accept the connection string.

Confirm the database is reachable

Even with correct properties, startup fails if PostgreSQL is not accepting connections. Test outside the application first:

bash
psql "host=localhost port=5432 dbname=appdb user=appuser password=secret"

If this cannot connect, Spring Boot will not either, and the problem is in the environment, not the Java configuration.

Typical causes:

  • PostgreSQL is not running
  • the port is different from what the app expects
  • firewall or container networking blocks the connection
  • the user lacks permission on the target database

Testing separately saves time because it takes Boot out of the equation.

Be careful with a manual DataSource bean

Many DataSource errors come from custom configuration classes. If you define your own DataSource bean, you are responsible for wiring it correctly:

java
1import javax.sql.DataSource;
2import org.springframework.boot.jdbc.DataSourceBuilder;
3import org.springframework.context.annotation.Bean;
4import org.springframework.context.annotation.Configuration;
5
6@Configuration
7public class DatabaseConfig {
8    @Bean
9    public DataSource dataSource() {
10        return DataSourceBuilder.create()
11            .url("jdbc:postgresql://localhost:5432/appdb")
12            .username("appuser")
13            .password("secret")
14            .build();
15    }
16}

This works, but overriding the bean means losing some of Boot's auto-configuration. If you do not need a custom bean, let Boot create the DataSource.

What each exception points to

  • ClassNotFoundException or driver-class errors usually mean a dependency problem
  • PSQLException about connection refusal usually means PostgreSQL is unreachable
  • authentication failures usually mean wrong credentials or server auth rules
  • bean creation errors around your custom config usually mean your own wiring is incomplete or conflicting

The top-level BeanCreationException is rarely enough. The useful clue is often several lines lower in the root cause chain.

A debugging sequence that works

  1. confirm the PostgreSQL driver dependency exists
  2. verify the spring.datasource properties that actually loaded
  3. test the connection with psql
  4. remove unnecessary custom DataSource beans
  5. read the root cause, not just the top-level Spring exception

This handles most startup failures quickly, without random changes to properties.

Mistakes that keep the error around

  • Setting driver-class-name and assuming that fixes it. If the dependency, URL or network is wrong, the error persists.
  • Adding a custom DataSource bean you do not need. It often disables Spring Boot behavior that would have worked out of the box.
  • Forgetting profile-specific configuration. application.properties may look correct while the app loads a different profile with different settings.
  • Debugging only through Spring logs. A direct psql test is often the fastest way to separate configuration issues from application issues.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design