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:

Using Micronaut CLI

Add Features

You can directly include database support and migration tool using:

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:

๐Ÿ—„ 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)

โœ… In-memory database โ€” no installation needed.

โœ… Fast startup.

โœ… Great for writing unit & integration tests.

Configuration for PostgreSQL

๐Ÿ”ง Make sure your PostgreSQL server is running.

๐Ÿ”ง Replace credentials with your actual database username & password.

Configuration for MySQL

๐Ÿ”ง 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:

  • 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