H2 Database Installation Guide
Requirements
- Java 11 or higher
- Any operating system that supports Java (Windows, Linux, macOS, etc.)
Installation Methods
Method 1: Download ZIP Package
- Visit the official website download page: https://www.h2database.com/html/download.html
- Download the "Platform-Independent Zip" version
- Extract the downloaded ZIP file to any directory, such as: C:\h2 or /opt/h2
Method 2: Maven Dependency
If you are using a Maven or Gradle Java project, you can add the dependency directly:
Maven:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.2.224</version>
</dependency>
Gradle:
implementation 'com.h2database:h2:2.2.224'
Method 3: Windows Installer
- Download the "Windows Installer" version
- Run the installer and follow the wizard to complete installation
Configure Environment Variables (Optional)
If you downloaded the ZIP package and want to use H2 directly from command line:
Windows:
- Right-click "This PC" → "Properties"
- Click "Advanced System Settings"
- Click "Environment Variables"
- Find "Path" in System Variables and click "Edit"
- Add the H2 bin directory path
Linux/macOS:
export PATH=$PATH:/opt/h2/bin
Verify Installation
Using Command Line
After extracting, run in command line:
java -jar h2.jar
This will start the H2 Console web interface.
Using JDBC Connection
Connect to H2 database in Java code:
// Embedded mode
String url = "jdbc:h2:mem:testdb";
Connection conn = DriverManager.getConnection(url, "sa", "");
// File database
String url = "jdbc:h2:file:./data/testdb";
Connection conn = DriverManager.getConnection(url, "sa", "password");
Using H2 Console
- Run command:
java -jar h2.jar - Open browser: http://localhost:8082
- Default username: sa, password is empty
Connection URL Formats
H2 supports various connection URL formats:
| Mode | URL Example | Description |
|---|---|---|
| In-memory | jdbc:h2:mem:testdb | Data stored in memory |
| Embedded file | jdbc:h2:file:./data/testdb | Data stored in file |
| TCP server | jdbc:h2:tcp://localhost:9092/testdb | Client-server mode |
| TCP encrypted | jdbc:h2:ssl://localhost:9092/testdb | SSL encrypted connection |
| PostgreSQL compatible | jdbc:h2:postgres://localhost:5432/testdb | PostgreSQL protocol compatible |
Common Issues
Port Already in Use
H2 uses port 9092 (TCP mode) and 8082 (Web Console) by default. If ports are occupied, you can specify other ports in the connection URL.
Java Version Issues
Make sure Java 11 or higher is installed. Check with:
java -version
Driver Not Found
Make sure the H2 JAR file is in the classpath. If using Maven project, ensure the dependency is correctly added.
