Setting Up a Micronaut App with H2 / PostgreSQL / MySQL

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

Setting Up a Micronaut App with H2 / PostgreSQL / MySQL

๐Ÿ”ง Why H2, PostgreSQL, and MySQL?

DatabaseUsage
H2Lightweight in-memory database, great for development & testing
PostgreSQLOpen-source, powerful RDBMS widely used in production
MySQLPopular 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

ProblemSolution
Database Connection FailedCheck database URL, username, and password
Driver class not foundEnsure correct driver dependency is added
Port already in useChange database or application port
H2 not accessibleUse 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.

๐ŸŒ External References