Fix "Failed to configure a DataSource: 'url' attribute is not specified" in Spring Boot
*************************** APPLICATION FAILED TO START *************************** Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver class Action: Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath. If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
- Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver class
- If you have database settings to be loaded from a particular profile you may need to activate it (the profiles dev are currently active).
- Cannot load driver class: org.postgresql.Driver
- If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
Spring Boot sees a database starter (JPA or JDBC) but finds neither spring.datasource.url nor an embedded database. Add your database URL, user and driver to application.properties, activate the right profile if the configuration lives in application-dev.properties, or remove the starter if the application needs no database.
What this error means
As soon as spring-boot-starter-data-jpa or spring-boot-starter-jdbc is on the classpath, auto-configuration tries to create a DataSource (a HikariCP pool). It needs a JDBC URL; failing that, it looks for an embedded database (H2, HSQLDB or Derby) in the dependencies. If neither is available, it cannot guess the driver and stops startup. The "Action" section gives both leads: add an embedded database or activate the profile holding the configuration. The "no profiles are currently active" note is a strong hint when your settings live in a profile file.
Common causes
- No database configuration: project generated with Spring Initializr with Spring Data JPA ticked, but
application.propertiesis empty. - Profile not active: the settings are in
application-dev.propertiesorapplication-prod.yml, but no profile is activated. - File misplaced or misnamed: outside
src/main/resources, typo (aplication.properties), wrong key (spring.datasource.uri) or badly indented YAML. - Missing environment variable: the URL comes from
SPRING_DATASOURCE_URLor a placeholder, set in production but not locally or in CI. - Starter added by mistake or pulled in by a dependency, while the application uses no database.
Step-by-step fix
-
Declare the connection
Add the JDBC URL, user and password to
src/main/resources/application.properties. Keep the password out of the repository with a placeholder fed by an environment variable.# PostgreSQL spring.datasource.url=jdbc:postgresql://localhost:5432/mydb spring.datasource.username=app spring.datasource.password=${DB_PASSWORD} # MySQL # spring.datasource.url=jdbc:mysql://localhost:3306/mydb -
YAML version
If you use
application.yml, keep the space indentation exact; a tab or a misalignment makes the key ignored. Validate the file when in doubt.spring: datasource: url: jdbc:postgresql://localhost:5432/mydb username: app password: ${DB_PASSWORD} -
Add the JDBC driver
Without a driver, "Failed to determine a suitable driver class" remains. Add your database driver as
runtimeOnly(Gradle) or withruntimescope (Maven); Spring Boot manages the version.// build.gradle.kts dependencies { runtimeOnly("org.postgresql:postgresql") // runtimeOnly("com.mysql:mysql-connector-j") } -
Activate the right profile
If the configuration is in
application-dev.properties, activate thedevprofile at launch, or throughSPRING_PROFILES_ACTIVE. In IntelliJ, fill in "Active profiles" in the run configuration.java -jar app.jar --spring.profiles.active=dev SPRING_PROFILES_ACTIVE=dev java -jar app.jar ./mvnw spring-boot:run -Dspring-boot.run.profiles=dev ./gradlew bootRun --args='--spring.profiles.active=dev' -
Tests and development: embedded database
For tests without a database server, add H2 as a test dependency: Spring Boot then configures an in-memory database automatically. To test against the real database, prefer Testcontainers with
@ServiceConnection.dependencies { testRuntimeOnly("com.h2database:h2") } -
No database needed? Remove the starter
The real fix is to remove
spring-boot-starter-data-jpa/spring-boot-starter-jdbc. If a dependency forces it, exclude the auto-configuration. In Spring Boot 4, the class isorg.springframework.boot.jdbc.autoconfigure.DataSourceAutoConfiguration.// Spring Boot 3 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Keep a default configuration that starts locally (or a
devprofile enabled inapplication.propertieswithspring.profiles.active=dev) and document the required variables. - Only tick the starters you need in Spring Initializr: each starter enables an auto-configuration that expects settings.
- Put passwords in environment variables or a secrets manager, never in plain text in a committed file.
Frequently asked questions
Why does the error only show up in tests or CI?
@SpringBootTest tests load the whole context with the same rules. If the URL comes from an environment variable missing in CI, add src/test/resources/application.properties with H2 or use Testcontainers.
My application.properties is correct but Spring does not read it, why?
Check that it is in src/main/resources and that it is copied to build/resources/main or target/classes. A custom <resources> block in pom.xml can exclude it; mvn clean or "Reload Gradle Project" often fixes an out-of-sync IDE.
What JDBC URL for MySQL, PostgreSQL or SQL Server?
MySQL: jdbc:mysql://localhost:3306/mydb. PostgreSQL: jdbc:postgresql://localhost:5432/mydb. SQL Server: jdbc:sqlserver://localhost:1433;databaseName=mydb;encrypt=true.
Useful tools for this error
More Java, Kotlin & .NET errors
- Fix "java.lang.NullPointerException: Cannot invoke … because … is null" in Java
- Fix "UnsupportedClassVersionError: compiled by a more recent version of the Java Runtime"
- Fix "Could not resolve all files for configuration" and "Could not find" in Gradle
- Fix "Web server failed to start. Port 8080 was already in use" in Spring Boot
- Fix "java.lang.OutOfMemoryError: Java heap space" (and GC overhead limit exceeded)
- Fix "Object reference not set to an instance of an object" (NullReferenceException) in C#
- Fix "lateinit property binding has not been initialized" in Kotlin (Android)