Configuring H2 database in Spring Boot

This article shows you how to configure the H2 database within a Spring Boot application.

Thanh Tran
Jan 4 · 3 min read

What is H2 database?

H2 database is an open-source in-memory database that can be easily embedded into Java application. H2 database provides a list of features such as Pure Java, memory mode, encrypted database, ODBC driver even full-text search. Its size approx around 2MB.

H2 database also provides a browser-based Console application to manage data.

h2 database browser-based Console application
h2 database browser-based Console application
H2 database features
H2 database features

H2 database configuration in spring boot

The first we need to do is add the H2 database to our classpath.


We only need the H2 database at runtime so we specify the scope to runtime.

We also need spring-boot-starter-data-jpa to pre-create database or we have to allow remote database creation(it effectively creates a remote security hole in your system).


Now we have all dependencies we need. Let specify some configuration to get the database actually works.

# H2 database connection info

In case you don’t want to use the password we can remove the spring.datasource.passwordproperty, or leave it blank.

By specifying spring.h2.console.enable=true it enables the browser-based Console application. After starting the application we accession via http://localhost:8080/h2-console/.

Image for post
Image for post

Through the console, we can easy to execute DML queries or DDL queries.

Addition H2 configuration properties:

# default value h2-console
# Enable trace output, default value: false
# Allowing remote access, default value: false
# password for accessing H2 Console Preferences
# and Administration Tools
Image for post
Image for post

The Preferences and Tools require web-admin-password to access.

Initialize tables and import data

In most popular situations require the creation of database tables, and data during the application starting process. Foutuanly, we just have to define DML and insert statements in a file named with data.sql and add it to classpath but we recommend that you define your database schema in a file named schema.sql and the insert statement in data.sql.


By reading to the end of this article, we have known how to config H2 for Spring boot application as well as some essential configuration h2 Console.


Source code:

Origin publish:

Give me motivation

To get new article update please follow our publication or follow us on social



Programming Sharing

We write about programming. We share about programming.

Sign up for Programming Sharings Newsletter

By Programming Sharing

Get new articles update by signing up our newsletter Take a look.

Check your inbox
Medium sent you an email at to complete your subscription.

Thanh Tran

Written by

Software Engineer at Terralogic. Blogger and Amateur Investor

Programming Sharing

The publication to share programming knowledge

Thanh Tran

Written by

Software Engineer at Terralogic. Blogger and Amateur Investor

Programming Sharing

The publication to share programming knowledge

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store