Excellent question. Your application.yaml is perfectly configured to allow you to do this. The key is the spring.h2.console.enabled: true property, which exposes a web-based console for the H2 database.
Here is a step-by-step guide to connect to your running H2 database.
Step 1: Run Your Spring Boot Application
First, make sure your application is running. You can start it from your IDE (like IntelliJ or VS Code) or by using the command line:
./mvnw spring-boot:run
# or if you use Gradle
./gradlew bootRun
Step 2: Access the H2 Console in Your Browser
Open your web browser and navigate to the path you configured. By default, Spring Boot runs on port 8080.
Go to: http://localhost:8080/h2
You will see a login page that looks like this:
Step 3: Fill in the Connection Details
This is the most crucial step, and where most people get tripped up. You need to use the exact same connection details that your Spring Boot application is using.
How to find the correct JDBC URL:
The easiest and most reliable way is to look at the console output when your application starts. Spring Boot helpfully prints the information you need. Look for a line similar to this:
H2 console available at '/h2'. Database available at 'jdbc:h2:mem:testdb'
Now, use that information to fill in the form:
- Driver Class: This should be pre-filled with
org.h2.Driver. Leave it as is.
- JDBC URL: This is the most important field.
- Copy the URL from your application's startup log.
- If you haven't configured a specific database name, Spring Boot's default in-memory URL is often
jdbc:h2:mem:testdb. Sometimes it can be a random UUID like jdbc:h2:mem:a2c4e6f-..... Always check your logs to be sure.
- User Name: The default username for H2 is
sa.
- Password: The default password is blank. Leave the password field empty.
Your completed form should look like this:
| Field | Value |
|---|
| Driver Class | org.h2.Driver |
| JDBC URL | jdbc:h2:mem:testdb |
| User Name | sa |
| Password | (leave this empty) |
Step 4: Connect
Click the Test Connection button first. If it says "Test successful", you're good to go!
Then, click the Connect button.
You will now be connected to your application's live, in-memory database. You can browse the schema, see the tables that JPA has created, and run SQL queries directly.
Important Considerations & Troubleshooting
-
"Table not found" Error: If you connect but see no tables, it almost certainly means you used the wrong JDBC URL. The H2 console and your application are two separate connections. If the database name in the URL (e.g., testdb) is different, the H2 console will connect to a new, empty in-memory database instead of the one your application is using. Double-check the URL from your startup logs.
-
Using Spring Security? If you have Spring Security in your project, it will likely block access to /h2 with a 403 Forbidden or 401 Unauthorized error. You need to explicitly permit access to the H2 console in your security configuration.
Here is a modern example using a SecurityFilterChain bean:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
import static org.springframework.boot.autoconfigure.security.servlet.PathRequest.toH2Console;
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
// This is the new, recommended way to allow H2 console access
.requestMatchers(toH2Console()).permitAll()
.anyRequest().authenticated()
)
// These two are needed to render the H2 console, which uses frames
.csrf(csrf -> csrf.ignoringRequestMatchers(toH2Console()))
.headers(headers -> headers.frameOptions().sameOrigin());
return http.build();
}
}
Note: For older Spring Security versions, the syntax might be .antMatchers("/h2/**").permitAll(). The toH2Console() method is preferred as it handles the path correctly even if you change it in application.yaml.
-
File-based H2 Database: If you were using a file-based database (e.g., spring.datasource.url=jdbc:h2:file:./mydatabase), you would simply use that exact same URL (jdbc:h2:file:./mydatabase) in the H2 console's JDBC URL field.