Java Spring Boot REST Server with PostgreSQL

🕓 45 minutes

What you’ll learn#

How to setup your application for :

  • connecting to PostgreSQL database,

In this tutorial, we will create a simple java component with Java Micronaut Data scaffolder with connection to PostgreSQL database storage.


Video example:

Project source#

This example project can be cloned from: [email protected]:innobank/aia-spring-data-db.git



Open your IDE, import created component and start coding:

  • Add these maven dependencies to your pom.xml file:

    01 <dependency>
    02 <groupId>javax.persistence</groupId>
    03 <artifactId>javax.persistence-api</artifactId>
    04 <version>2.2</version>
    05 </dependency>
  • Define jpa entity Client. This simple table will store basic client data:

    • Generate getters and setters with your IDE

      01 package org.example.service.repository.entity;
      03 import java.time.LocalDate;
      05 import javax.persistence.Entity;
      06 import javax.persistence.GeneratedValue;
      07 import javax.persistence.Id;
      09 @Entity
      10 public class Client {
      11 @Id
      12 @GeneratedValue
      13 private Long id;
      15 private String username;
      16 private String firstname;
      17 private String surname;
      18 private LocalDate birthdate;
      19 }
  • Next prepare database configuration:

    • Go to the PgAdmin console (http://localhost:5050 if using compose-postgre from our Local development manual) and create a new db client-db with the scheme client-data. Now change the configuration in config/application.yaml:
    • Fill {db user} and {db password} according to your configuration
    • Make sure you follow yaml syntax (especially whitespaces)

What’s next?#

If your code works in the local development, you are ready to push your changes to GIT and try to build and deploy your new component version to the CodeNOW environment. For more information, see Application Deployment and Monitoring, just make sure to change the application.yaml properties from the local to the production setup.