Skip to content
  1. LuzineDebug
  2. Java, Kotlin & .NET
  3. ***************************
Java, Kotlin & .NET Beginner ≈ 10 min to fix

Fix "Failed to configure a DataSource: 'url' attribute is not specified" in Spring Boot

Error message
***************************
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.
In short

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.

Be the first to confirm this fix

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

  1. No database configuration: project generated with Spring Initializr with Spring Data JPA ticked, but application.properties is empty.
  2. Profile not active: the settings are in application-dev.properties or application-prod.yml, but no profile is activated.
  3. File misplaced or misnamed: outside src/main/resources, typo (aplication.properties), wrong key (spring.datasource.uri) or badly indented YAML.
  4. Missing environment variable: the URL comes from SPRING_DATASOURCE_URL or a placeholder, set in production but not locally or in CI.
  5. Starter added by mistake or pulled in by a dependency, while the application uses no database.

Step-by-step fix

  1. 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.

    ini
    # 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
  2. 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.

    yaml
    spring:
      datasource:
        url: jdbc:postgresql://localhost:5432/mydb
        username: app
        password: ${DB_PASSWORD}
  3. Add the JDBC driver

    Without a driver, "Failed to determine a suitable driver class" remains. Add your database driver as runtimeOnly (Gradle) or with runtime scope (Maven); Spring Boot manages the version.

    kotlin
    // build.gradle.kts
    dependencies {
        runtimeOnly("org.postgresql:postgresql")
        // runtimeOnly("com.mysql:mysql-connector-j")
    }
  4. Activate the right profile

    If the configuration is in application-dev.properties, activate the dev profile at launch, or through SPRING_PROFILES_ACTIVE. In IntelliJ, fill in "Active profiles" in the run configuration.

    bash
    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'
  5. 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.

    kotlin
    dependencies {
        testRuntimeOnly("com.h2database:h2")
    }
  6. 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 is org.springframework.boot.jdbc.autoconfigure.DataSourceAutoConfiguration.

    java
    // 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 dev profile enabled in application.properties with spring.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

All Java, Kotlin & .NET errors →