Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What is Spring Boot's default database?

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 15, 2024
UpdatedNov 16, 2026
4 min read

Spring Boot does not have a default database in the sense of always creating one. It only auto-configures an embedded database, usually H2, when no DataSource is configured and an embedded database driver (H2, HSQLDB or Derby) is on the classpath. If you add only spring-boot-starter-data-jpa and no driver, the application fails at startup with a "Failed to determine a suitable driver class" error.

When an embedded database is used, it runs in memory, so its data is lost when the application stops. This is useful for tests and prototypes, but you should configure a real database such as PostgreSQL or MySQL for anything you deploy.

Table of Contents

  • When Spring Boot auto-configures an embedded database
  • Adding H2 for a quick start
  • Configuring the H2 database and its console
  • Switching to a production database
    • Using the same database as production in tests
  • Frequently Asked Questions
    • Is H2 the default database in Spring Boot?
    • Why does my Spring Boot app fail with "Failed to determine a suitable driver class"?
    • Is data kept after restarting an H2 in-memory database?
    • Should I use H2 in production?
  • Summary

When Spring Boot auto-configures an embedded database

Spring Boot's DataSourceAutoConfiguration looks for a DataSource bean and for spring.datasource.* properties. If neither exists, it checks whether one of the supported embedded databases is on the classpath. The supported ones are H2, HSQLDB and Apache Derby. If it finds one, it creates an in-memory DataSource for you.

This means the "default" depends entirely on your dependencies:

SituationResult
No JDBC driver on the classpathStartup fails when a DataSource is required
Only H2 on the classpath, no propertiesIn-memory H2 database is created
H2 plus spring.datasource.url for PostgreSQLYour configured PostgreSQL database is used
PostgreSQL driver but no URLStartup fails, because Boot does not guess a server address

Adding H2 for a quick start

Add the starter and the H2 driver as a runtime dependency. With Maven:

xml

With no other configuration, a JPA entity and a repository work immediately:

java

For an embedded database Spring Boot also sets spring.jpa.hibernate.ddl-auto to create-drop, so Hibernate creates the tables at startup and drops them at shutdown. For a non-embedded database the default is none, which is why you must manage the schema yourself there.

Configuring the H2 database and its console

By default Boot generates a unique in-memory database name on each start. To use a stable name, or to keep data in a file, set the URL yourself in application.properties:

properties

With spring.h2.console.enabled=true and the spring-boot-devtools or web starter present, you can open /h2-console in the browser and run SQL against the running database. Enable the console only in development, because it exposes your data.

To keep data between restarts, use a file URL such as jdbc:h2:file:./data/demo. H2 is then still embedded, but it persists to disk.

Switching to a production database

Replace the H2 dependency with the driver you need and set the connection properties. For PostgreSQL:

properties

Boot detects the driver class from the URL, so spring.datasource.driver-class-name is rarely needed. Use validate or none with a migration tool such as Flyway or Liquibase, both of which Boot configures automatically when they are on the classpath.

Using the same database as production in tests

H2 can behave differently from PostgreSQL or MySQL in SQL dialect, locking and types. For realistic tests, use Testcontainers with @ServiceConnection (Spring Boot 3.1 and later), which starts a real database in a container and wires the connection automatically:

java

Note that @DataJpaTest replaces your configured DataSource with an embedded one by default. Add @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) if you want it to use the real or containerised database.

Frequently Asked Questions

Is H2 the default database in Spring Boot?

Only in effect. Boot has no built-in database, but if H2 is the embedded driver on the classpath and you configure nothing else, it creates an in-memory H2 database.

Why does my Spring Boot app fail with "Failed to determine a suitable driver class"?

No spring.datasource.url is set and no embedded database is on the classpath. Add a driver and a URL, or add H2 for local development.

Is data kept after restarting an H2 in-memory database?

No. In-memory data lives in the JVM and disappears at shutdown. Use a file URL or a real database to persist it.

Should I use H2 in production?

No. Use a server database such as PostgreSQL or MySQL, and use H2 or a Testcontainers database only for development and tests.

Summary

Spring Boot has no default database of its own. It auto-configures an embedded in-memory database such as H2 only when the driver is on the classpath and no DataSource is configured. Set spring.datasource.* for real databases, manage the schema with Flyway or Liquibase, and prefer Testcontainers when your tests need production-like behaviour.

Share

Similar posts

  • What Is an Exception in Java? Checked vs Unchecked Exceptions
  • What Are Sealed Interfaces and Records Together in Java Domain Models?
  • Association vs Aggregation vs Composition in Java Explained
  • What Is Composition vs Inheritance in Java and Which Should You Prefer?
  • What Is the equals and hashCode Contract in Java?
  • What Is the Object Class in Java? equals, hashCode and toString
  • What Is Method Overriding in Java and How Is It Different from Overloading?
  • What Is Polymorphism in Java? Compile-Time vs Runtime