Featured image of post Integrating PostgreSQL Database

Integrating PostgreSQL Database

Learning Micronaut Journey - Part 5. Learn the essentials of Micronaut Data: a step-by-step journey into uniting a PostgreSQL Database.

Introduction

In this blog post, we will explore how to seamlessly integrate a PostgreSQL database into a Micronaut microservices environment using Docker Compose.

Why Docker Compose?

Docker Compose simplifies the process of running multi-container Docker applications. It allows you to define and manage your application’s services, networks, and volumes in a single docker-compose.yml file, making it easier to reproduce the entire development environment.

Setting the Stage

Before we dive into the integration process, ensure you have Docker and Docker Compose installed on your machine. Once you have them ready, let’s proceed with the integration.

Create a Micronaut Project

We will continue the project from the previous blog.

Define Docker Compose Configuration

Create a docker-compose.yml file in the root of your project:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
version: '3.6'
services:
  db:
    image: postgis/postgis:14-3.3
    restart: always
    environment:
      POSTGRES_USER: local
      POSTGRES_PASSWORD: local
      POSTGRES_DB: local
    command: postgres -N 500
    ports:
      - "5432:5432"
    volumes:
      - type: tmpfs
        target: /var/lib/postgresql/data

This configuration defines a PostgreSQL service with a database named local and a user with the credentials local and local.

Configure Micronaut to Use PostgreSQL

Usually, you need to update your application.yml file in the src/main/resources directory, something like below:

1
2
3
4
5
6
datasources:
  default:
    url: jdbc:postgresql://localhost:5432/local
    driverClassName: org.postgresql.Driver
    username: local
    password: local

This configuration informs Micronaut about the PostgreSQL database and its connection details.

However, we will connect our Micronaut Application with PostgreSQL in the next blog.

Run PostgreSQL with Docker Compose

Navigate to your project’s root directory and run the following command:

1
docker compose up -d

This command starts the PostgreSQL container.

Verify the Integration

We will use the pgAdmin 4 tool to illustrate a database:

Create a new Server

Fill in the Server name

Fill in the Server credentials

Connected to the local Server

Conclusion

Integrating a PostgreSQL database with Micronaut using Docker Compose is a straightforward process. This setup allows you to focus on building your microservices while ensuring a consistent and reproducible development environment.

Explore further by extending your Micronaut application, implementing data access, and unleashing the power of microservices with a robust database backend.

Happy coding!

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy