Tutorial: Learn how to access H2 Database console from a browser. 🤔
This DB dev guide provides software engineers with a step-by-step walkthrough regarding how to programmatically embed the H2 Database Console in a Java application as well as how to access the embedded H2-console from a web browser.
H2 Database Console Access TOC
The code in the images provided in this instructional is written in the Groovy Scripting Language (Groovy) and utilizes the Groovy Console.
We programmatically create a database called example-db which exists in-memory only.
The example-db database will only exist while the example Groovy script is executing — once the script exits, the database will no longer exist.
Using the H2 database as an embedded in-memory only relational database is a use case that appears with some frequency and which provides software developers with considerable power for managing data internally in their application.
Updated: June 05, 2025.
CTO Strategy Consulting for Growing Technology Teams
Configure H2 DB With Spring Boot Now!
How to access the H2 Database from the command line
We can open the H2 Database console from the command line using the startup script or by simply running the executable jar file and passing in several parameters.
java -cp ./h2-2.2.224.jar org.h2.tools.Server -ifNotExists -webPort 19999 -webSSL
In this example there are several parameters that will appear again when we expose H2 programmatically — they are:
webPort
The H2 Database console will be available on port 19999.
webAllowOthers
Not in use here!
The webAllowOthers parameter allows network connections from other computers to the H2 web console.
Since we’re running in server mode, allowing others to connect to the H2 Database remotely raises a security concern (see also: server mode not support -ifNotExists option #1792 on GitHub).
webSSL
The webSSL parameter for the H2 Database enables SSL encryption for the H2 Console, making connections to it secure.
Network with CTOs in the DC Metro Area
ifNotExists
From the H2 Database Server’s main method JavaDoc this option creates databases when they are accessed — if we do not include this option then we’ll see the following error when we click the connect button:
Database “mem:example-db” not found, either pre-create it or allow remote database creation (not recommended in secure environments) [90149-224] 90149/90149 (Help)
See also:
The following image demonstrates what should happen when this command is executed:
The H2 Database webSSL parameter will require the Chrome browser (Chrome) to use HTTPS however as the certificate will be flagged as invalid we’ll need to override this in the Chrome Advanced options.
After we’ve clicked on the “Advanced” button in Chrome we should see a link to proceed to localhost, which is considered by Chrome to be unsafe.
The following page should look something like what I’ve included below.
The password in this example has been masked, however I set it to “sa”.
Once this is done smash the “Connect” button in order to progress to the next page.
Once we’ve connected to the database we should see the console as it appears below.
The pointer in the image below directs our attention to the example-db in-memory database.
In the next section we’ll take a look at how we can expose the H2 Database web console programmatically.
Exporting the H2 Database console programmatically may be helpful when developing and testing software.
How to programmatically expose the H2 Database console
In this section I’ll walk you through how to programmatically expose the H2 Database so that the console can be accessed via a web browser.
Step One: Add the H2 dependency to your project.
In the script we have a reference to the H2 database dependency which is pulled from MvnRepository— this jar file is required in order to run this example.
Step Two: Import the org.h2.tools.Server class.
We need to import the org.h2.tools.Server class, which facilitates the configuration of the H2 Console web, TCP, and PG server.
Exposing the H2 Console can be particularly useful for development and testing environments, where direct access to the embedded database through different protocols and interfaces can significantly streamline database operations and management.
Step Three: Create a connection to an example in-memory database.
The following line of Groovy code (refer to the red pointer) establishes a connection to an H2 database using Java Database Connectivity (JDBC).
Let’s review what each part of the line does:
def connection
This declares a variable named connection in the Groovy Programming Language which will hold a reference to the database connection object returned by the DriverManager.getConnection method.
DriverManager.getConnection(…)
The getConnection method is part of the JDBC API and is used to establish a connection to a database.
The getConnection method returns an instance of java.sql.Connection, which represents a connection to the specified database.
jdbc:h2:mem:example-db
This string is the JDBC URL to the H2 database and it specifies several things:
- jdbc:h2 — This indicates that the JDBC connection is for an H2 database.
- mem — This specifies that the database is in-memory. An in-memory database resides entirely in memory and is not persisted to disk, making it very fast but volatile in that data is lost when the application is stopped.
- example-db — This is the name of the in-memory database.
- “sa” — This is the username for the database connection.
- “sa” — This is the password for the database connection.
The in-memory nature of the database makes it ideal for testing and development purposes, where persistence between application restarts is not required.
Step Four: Obtain a reference to the org.h2.tools.Server and then start the server.
Step four requires us to acquire a reference to the org.h2.tools.Server by invoking the createWebServer method and then calling the start method on the server.
WARNING: The call to the createWebServer method in the image below contains the webAllowOthers argument, which may be appropriate when used temporarily for testing purposes however it should not be included in production software — refer to the following links for more information:
As stated in the comments in the script, the arguments passed to the createWebServer method include:
Step Five: Get the URL that the web frontend.
We need to get the URL that the web frontend and we do this by invoking the getURL method.
Note that it may be useful to include the URL in a message sent to a log file when working on a web application in a headless Linux environment, for example.
This step may be treated as optional once we know the URL the H2 Database is available on.
Step Six: Open a browser using the URL.
We use the URL obtained in the previous step and pass it as a parameter when we call the openBrowser method.
This step is optional since we may not want to open a browser at the time the program is run — this may make sense when, for example, we’re running a web application and the desktop environment is not available.
Step Seven: Stop the Server.
When the application is being terminated we close the connection and invoke the server’s stop method.
Note that the call to both connection.close and server.stop should be located inside of a finally block rather than the catch block shown here.
Step Eight: Login to the H2 Database Console.
Login to the H2 Database Console using the username “sa” and password “sa” and then click the “Connect” button.
Note that the username and password are set on line #18 in the script so if you need to change these, the modification will need to be made there.
Step Nine: Browse the H2 in-memory Database.
When the example script is executed, a browser window should be opened that looks something like what I’ve included below.
The H2 Database Console is a powerful web interface that allows for direct interaction with H2 database.
The H2 Database console provides a user interface that the software developers can use to execute SQL queries, update data, create and modify tables, and perform various database management tasks all through their browser.
In the image below we can see that the example-db database that we created on line 18 in the Groovy script is visible in the upper left-hand side of the H2 Database console webpage.
Note that there is no data currently in the example-db database — if we want to add or inspect tables or data we can do that here.
Example Script used to expose the h2 database console
In this section I’ve included the example Groovy script used to programmatically expose the h2 database console so that it can be accessed via a browser.
package com.thospfuller
/**
* Tutorial regarding how to configure the H2 Database web server programmatically.
*
* Note that if we don't use the systemClassLoader we'll get the following exception:
*
* java.sql.SQLException: No suitable driver found for jdbc:h2:mem:example-db
*/
@GrabConfig(systemClassLoader=true)
@Grapes(
@Grab(group='com.h2database', module='h2', version='2.3.232')
)
import java.sql.DriverManager
import org.h2.tools.Server
def connection = DriverManager.getConnection("jdbc:h2:mem:example-db", "sa", "sa")
/* webPort : H2 console port, set to 19999 for this example.
*
* webSSL : HTTPS connections.
*/
def server = Server.createWebServer("-webPort", "19999", "-webSSL").start()
def url = server.getURL ()
def minutes = 60L
println "Server URL: ${url}, will sleep for $minutes minutes."
Server.openBrowser (url)
try {
Thread.currentThread ().sleep (1000 * 60 * minutes)
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace (System.err)
} finally {
connection.close ()
server.stop ()
}
println "...done!"
return
You should be able to run this script as-is using the Groovy Console.
Keep in mind that the main thread sleeps for only one hour and once it resumes the script will terminate.
The tutorial conclusion follows.
Mastering H2 Triggers: Create Powerful Event-Driven Database Logic
Conclusion
I hope that this instructional has provided adequate guidance regarding how to programmatically embed the H2 Database in a Java application as well as how to access the H2 Database from a browser.
If you encounter any problems or have questions about this tutorial, please leave a comment and I’ll try to help.
Learn more about database change notifications — a hidden software development gem supported by some relational databases.
See Also
- How to use Groovy scripting
- The Java Code Examples Category
Frequently Asked Questions (FAQ)
The term “headless Linux” refers to a Linux operating system setup that lacks a graphical user interface (GUI).
The headless Linux instance is managed remotely via the command line interface (CLI) using tools such as SSH, thereby making it ideal for servers, embedded systems, and appliances where a GUI is both unnecessary as well as typically unavailable.
Yes, the H2 Database is a relational database.
Add the -ifNotExists flag when starting the h2 database — for example:
java -cp ~/.groovy/grapes/com.h2database/h2/jars/h2-2.3.232.jar org.h2.tools.Server -ifNotExists -web -browser -baseDir ~/h2-database/
Refer to the following section for further details about how to address the “either pre-create it or allow remote database creation” error in the h2 database.
Starting the h2 database with both the -ifNotExists flag and assign a base directory:
java -cp ~/.groovy/grapes/com.h2database/h2/jars/h2-2.3.232.jar org.h2.tools.Server -ifNotExists -web -browser -baseDir ~/h2-database/
WARNING: If you do not include the -ifNotExists flag, you will not be able to log in and the ‘either pre-create it or allow remote database creation’ error will appear in the h2 web UI.











