What is Spring Boot's default database?
Table of Contents
Introduction
Spring Boot is designed to make application development easier by providing auto-configuration for many common use cases, including database connections. When it comes to databases, Spring Boot has a default database, which is used when no explicit configuration is provided by the developer. Understanding what Spring Boot uses by default helps you to make decisions about how to configure your application, especially in the early stages of development or when testing.
By default, Spring Boot uses an in-memory H2 database when no other database is configured. However, this can be easily overridden to connect to other databases such as MySQL, PostgreSQL, or any other supported relational database.
Spring Boot's Default Database: H2
1. In-Memory H2 Database
When you create a Spring Boot application and do not specify a database connection, Spring Boot automatically configures an H2 database as the default choice. H2 is a lightweight, in-memory relational database written in Java, and it is often used for development and testing due to its simplicity and ease of setup.
Key characteristics of the H2 database:
- In-Memory: The default setup uses an in-memory database, meaning all data is stored in RAM and is lost when the application stops.
- Embedded: It does not require an external database server, making it ideal for local development and testing.
- SQL-Compatible: H2 supports standard SQL queries, making it a suitable replacement for more heavyweight databases like MySQL or PostgreSQL during development.
By default, Spring Boot uses an H2 database when you run the application in the absence of a data source configuration.
Example of default configuration:
With the above default configuration:
- spring.datasource.url points to an in-memory H2 database (
jdbc:h2:mem:testdb
). - spring.jpa.database-platform is set to
org.hibernate.dialect.H2Dialect
, indicating the use of H2 with Hibernate.
2. Benefits of Using H2 in Development
- Quick Setup: You don’t need to install or configure a separate database server, making it convenient for development and testing.
- Lightweight: The in-memory nature of the database means no disk space is used unless you explicitly configure a persistent H2 database.
- Automatic Schema Generation: Spring Boot can automatically generate database schemas based on your JPA entities, so there’s no need to manually create tables.
3. Overriding the Default Database
If you want to connect Spring Boot to an external database (like MySQL, PostgreSQL, etc.), you need to explicitly configure the data source. This typically involves setting up the database connection URL, username, password, and dialect in the application.properties
or application.yml
file.
For example, to configure Spring Boot to use MySQL instead of the default H2 database, you would add the following configuration:
Here, the spring.datasource.url
points to a MySQL server instead of the in-memory H2 database, and the spring.jpa.database-platform
is set to org.hibernate.dialect.MySQL8Dialect
for MySQL compatibility.
4. Persisting H2 Data (Optional)
While Spring Boot defaults to an in-memory H2 database, you can persist the data to disk by changing the spring.datasource.url
configuration:
This configuration will store the database on disk in the ./data/mydb.mv.db
file, so the data will persist across application restarts.
Conclusion
By default, Spring Boot uses an H2 in-memory database when no other database configuration is provided. This makes it ideal for quick prototypes, development, and testing. However, Spring Boot also offers easy configuration for connecting to external databases like MySQL, PostgreSQL, and others, should your application require a production-level database.
Spring Boot’s default database configuration helps streamline development by providing an automatic and simple setup, allowing you to focus on building features rather than managing the database configuration.