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:
For Gradle:
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:
Equivalent YAML:
Common mistakes:
- using
jdbc:postgres://instead ofjdbc: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:
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:
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
ClassNotFoundExceptionor driver-class errors usually mean a dependency problemPSQLExceptionabout 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
- confirm the PostgreSQL driver dependency exists
- verify the
spring.datasourceproperties that actually loaded - test the connection with
psql - remove unnecessary custom
DataSourcebeans - 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-nameand assuming that fixes it. If the dependency, URL or network is wrong, the error persists. - Adding a custom
DataSourcebean you do not need. It often disables Spring Boot behavior that would have worked out of the box. - Forgetting profile-specific configuration.
application.propertiesmay look correct while the app loads a different profile with different settings. - Debugging only through Spring logs. A direct
psqltest is often the fastest way to separate configuration issues from application issues.
Related reading
- Exchanging data between Android SQL-Lite and SQL Sever without using webserive
- Exclude a column using SELECT * [except columnA] FROM tableA?
- Executing an update/delete query in the JQPL query
- Existing tools to find unused tables in cassandra cluster
- Exceptions in rabbitmq with spring boot
- exclude Component from ComponentScan
- Exception when processing data during Kafka stream process
- Exception when trying to use tensorflow classify android example on a model trained from scratch
System design interview problems on this topic
- Design a Distributed OLTP Database: solution and walkthrough
- Design a Wide Column Database: solution and walkthrough
- Design a Key Value Store: solution and walkthrough
- Design a Web Cache: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.