Postgres SQL with Micronaut on Heroku

In the earliar post Micronaut application in heroku, we have created a simple micronaut application and deployed to heroku.

Now lets add a database to persist data using Postgres SQL, a powerful, open source object-relational database system.

If you don’t have the database installed, follow the steps mentioned in official site.

Now lets update our application .

Add required dependencies (build.gradle)

  1. micronaut-data is a database access toolkit that uses Ahead of Time (AoT) compilation to pre-compute queries for repository interfaces that are then executed by a thin, lightweight runtime layer.
  2. hikari is a fast, simple, reliable. HikariCP is a “zero-overhead” production ready JDBC connection pool.
  3. flyway makes database migrations easy.
  4. postgresql

Update application configuration (application.yml)

Now that the application is ready to use the postgres database. Lets create some tables , add some test data and create an endpoint to expose the data.

Database migrations

Create folders db/migrations inside resources (default path flyway looks into)

V1__schema.sql : creates tables

V2__test_data.sql : adds test data

Lets write simple code to fetch data from database.

Create packages domain, model, repository, service and create classes

domain -> StudentGrades

domain -> Subject

model -> Student

repository -> StudentRepository

service -> StudentService

resource -> StudentResource

Now our application is ready . Lets build , run and test

Lets us now prepare to deploy and run in heroku.

Heroku provides postgres as an add-on

Lets create a database from heroku-cli

This creates an environment variabe DATABASE_URL. We can check environement variables using heroku-cli

We cannot use AS-IS in micronaut, so lets create new env variables via heroku-cli. Commands to get , set , delete variables

Now lets set the variables defined in application.yml , from DATABASE_URL value.

Lets check our values are present and have right values

Now our environment in heroku is ready to run our new application.

Commit , push and deploy the application.

Now we have a micronaut application with postgres database running in heroku .

Happy Learning.



Software engineer, technology enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store