Skip to main content
Spring Boot4 min read2026-03-01

Spring Boot Failed to Configure DataSource

Fix 'Failed to configure a DataSource: url attribute is not specified and no embedded datasource could be configured'.

Error Code / Stack Trace

Action: Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.

Problem Overview

Spring Boot added Spring Data JPA to the classpath but cannot find JDBC connection properties or an in-memory database driver.

Why Does This Happen?

  • spring-boot-starter-data-jpa is in pom.xml, but spring.datasource.* properties are missing.
  • No in-memory database dependency like H2 is added for local testing.
  • The application properties file is misspelled (e.g., application.prop instead of application.properties).

Step-by-Step Solution

Step 1: Add JDBC properties to application.properties

Specify the database URL, username, password, and driver class name.

properties
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=postgres
spring.datasource.password=secret
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto=update

Step 2: Or disable DataSource autoconfiguration if DB is not needed yet

If you don't need a database connection yet, exclude DataSourceAutoConfiguration.

java
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Common Mistakes to Avoid

  • Adding spring-boot-starter-data-jpa before provisioning or deciding on a database.
  • Using environment variable names with hyphens that Spring Boot cannot bind to properties.

Prevention & Best Practices

  • Use test profiles (application-test.properties) with H2 in-memory DB for automated builds.

Frequently Asked Questions

How do I use H2 for local testing without full PostgreSQL?

Add com.h2database:h2 dependency to your pom.xml/build.gradle and Spring Boot will auto-wire an in-memory database.