Micronaut makes building modern cloud-native applications extremely simple, but one of the most important parts of any application is data persistence โ saving and retrieving data from a database.
In this post, we will learn how to create a Micronaut project and connect a Micronaut App with H2 / PostgreSQL / MySQL

๐ง Why H2, PostgreSQL, and MySQL?
| Database | Usage |
|---|---|
| H2 | Lightweight in-memory database, great for development & testing |
| PostgreSQL | Open-source, powerful RDBMS widely used in production |
| MySQL | Popular and mature RDBMS, supported by many hosting providers |
By learning how to configure all 3, youโll be able to easily switch between development, test, and production environments.
๐ฆ Prerequisites
Before you begin, make sure you have:
- โ Java 17 or higher (Java 21 is even better)
- โ Micronaut CLI installed โ optional but very helpful
- โ Maven installed
- โ PostgreSQL or MySQL server running (for real database connection)
Check Installing Micronaut CLI and Setting Up Your First Project
๐จ Step 1: Create Your Micronaut Project
We will create a project for our example School Management System using package name:
com.kscodes.micronaut.school
Using Micronaut CLI
mn create-app com.kscodes.micronaut.school --build=maven --lang=java
Add Features
You can directly include database support and migration tool using:
mn create-app com.kscodes.micronaut.school --features=data-jdbc,jdbc-hikari,mysql,postgres,h2,flyway --build=maven
Alternative: Use Micronaut Launch
You can also use Micronaut Launch to generate the project visually.
๐ Step 2: Add Maven Dependencies (Manual Option)
If you are manually modifying your project, ensure your pom.xml contains:
io.micronaut
micronaut-runtime
io.micronaut.data
micronaut-data-jdbc
io.micronaut.sql
micronaut-jdbc-hikari
com.h2database
h2
runtime
mysql
mysql-connector-java
runtime
org.postgresql
postgresql
runtime
io.micronaut.flyway
micronaut-flyway
๐ Step 3: Configure Database Connection
Micronaut uses application.yml or environment-specific YAML files to configure datasources.
๐ฅ Tip: You can easily switch between databases by commenting/uncommenting the datasource you want.
Configuration for H2 (for development & testing)
datasources:
default:
url: jdbc:h2:mem:devDb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
username: sa
password:
driverClassName: org.h2.Driver
dialect: H2
micronaut:
application:
name: school-management
โ In-memory database โ no installation needed.
โ Fast startup.
โ Great for writing unit & integration tests.
Configuration for PostgreSQL
datasources:
default:
url: jdbc:postgresql://localhost:5432/schooldb
username: your_postgres_user
password: your_postgres_password
driverClassName: org.postgresql.Driver
dialect: POSTGRES
micronaut:
application:
name: school-management
๐ง Make sure your PostgreSQL server is running.
๐ง Replace credentials with your actual database username & password.
Configuration for MySQL
datasources:
default:
url: jdbc:mysql://localhost:3306/schooldb
username: your_mysql_user
password: your_mysql_password
driverClassName: com.mysql.cj.jdbc.Driver
dialect: MYSQL
micronaut:
application:
name: school-management
๐ง MySQL 8+ recommended.
๐ง Use MySQL Workbench or CLI to create the database if not already created.
๐ Recommended Directory Structure
We will follow this package structure:
com.kscodes.micronaut.school
โโโ controllers
โโโ entities
โโโ repositories
โโโ services
- controllers โ REST API endpoints
- entities โ Database entity classes
- repositories โ Database access layer
- services โ Business logic
๐ This structure keeps your code clean and maintainable.
โก Step 4: Running the Application
You can run the Micronaut app using Maven:
mvnw mn:run
โ If your configuration is correct, Micronaut will successfully start and connect to the database.
๐ Common Issues and Troubleshooting
| Problem | Solution |
|---|---|
Database Connection Failed | Check database URL, username, and password |
Driver class not found | Ensure correct driver dependency is added |
| Port already in use | Change database or application port |
| H2 not accessible | Use proper H2 JDBC URL format |
๐งช Why Use H2 for Local Development?
- Faster feedback loop.
- No external dependency.
- Easily resettable database.
You can later switch to PostgreSQL/MySQL for integration or production.