Spring Boot
Auto-Configuration
DataSource
Database Configuration
Error Handling

Failed to auto-configure a DataSource 'spring.datasource.url' is not specified

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The error Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified means Spring Boot thinks your application needs a JDBC DataSource, but it does not have enough configuration to create one. The fix is not always “add a URL.” First determine why Boot is trying to configure a database at all.

Why Spring Boot Tries to Create a DataSource

Spring Boot auto-configures a DataSource when it sees JDBC- or JPA-related dependencies on the classpath. For example, if the project includes spring-boot-starter-data-jpa and a database driver, Boot assumes you want database access.

If the application lacks the required connection properties, startup fails.

Typical required properties look like this:

properties
1spring.datasource.url=jdbc:postgresql://localhost:5432/appdb
2spring.datasource.username=appuser
3spring.datasource.password=secret
4spring.datasource.driver-class-name=org.postgresql.Driver

If you really want a database, adding those properties is the correct fix.

Fix 1: Configure the Database Properly

For a real JDBC-backed application, put the datasource configuration in application.properties or application.yml.

yaml
1spring:
2  datasource:
3    url: jdbc:mysql://localhost:3306/appdb
4    username: appuser
5    password: secret
6    driver-class-name: com.mysql.cj.jdbc.Driver

Then make sure the actual driver dependency is present and the database is reachable.

Fix 2: Use an Embedded Database for Local Development

If you only need a quick local database, add an embedded database dependency such as H2. Spring Boot can auto-configure that without a manually supplied URL in many development setups.

Example dependency in Maven:

xml
1<dependency>
2    <groupId>com.h2database</groupId>
3    <artifactId>h2</artifactId>
4    <scope>runtime</scope>
5</dependency>

This is useful for demos, tests, and simple local runs.

Fix 3: Remove Unneeded Database Starters

Sometimes the real answer is that the application does not need a datasource at all. If you added spring-boot-starter-data-jpa or another JDBC-oriented starter by accident, remove it.

For example, a REST API that does not talk to a database should not carry JPA dependencies just because they were copied from another project.

This is often the cleanest solution.

Fix 4: Exclude DataSource Auto-Configuration

If the dependency must remain but you do not want Boot to create a datasource in this application, exclude the auto-configuration explicitly.

java
1import org.springframework.boot.SpringApplication;
2import org.springframework.boot.autoconfigure.SpringBootApplication;
3import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
4
5@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
6public class Application {
7    public static void main(String[] args) {
8        SpringApplication.run(Application.class, args);
9    }
10}

This tells Boot not to create a DataSource automatically.

How to Decide Which Fix Is Right

Ask one question first: should this application have a relational datasource?

  • If yes, configure spring.datasource.* correctly
  • If only for local testing, consider H2
  • If no, remove the database starter or exclude datasource auto-configuration

That decision avoids treating every startup error as a property typo.

It is also worth checking the active Spring profile. A common setup keeps datasource properties in a profile-specific file such as application-dev.yml or application-prod.yml. If the expected profile is not active, Boot starts without the database settings you thought were present.

Common Pitfalls

A common mistake is adding the database driver but forgetting the URL, username, or password.

Another mistake is copying spring-boot-starter-data-jpa into a project that does not need relational persistence. That makes Boot try to configure a datasource for no reason.

A third issue is excluding datasource auto-configuration when the application actually needs a database later. That hides the real configuration problem instead of solving it.

Summary

  • The error means Spring Boot expects to create a JDBC datasource but lacks enough configuration
  • If the app needs a database, add the correct spring.datasource.* properties
  • For simple local development, an embedded database such as H2 can be enough
  • If the app does not need a datasource, remove the starter or exclude the auto-configuration
  • Fix the cause of Boot's expectation, not just the symptom of the startup error

Course illustration
Course illustration

All Rights Reserved.