Java Spring Boot Tutorial…

In this tutorial, I will guide you on how to connect Spring Boot and PostgreSQL. Previously I have shown you how to perform CRUD operations with Spring Boot and MySQL.

Let us follow the steps below.

Prerequisites

  • Install Spring Boot into VS Code
  • Install PostgreSQL - Download the setup from here.
  • Install pgAdmin 4 - You can download it from here

Create a new Spring Boot Project

Step one is to create a new Spring Boot project. Here is an example;

Add the required Dependencies to the Pom.xml

You need to add the following dependencies.

<dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <scope>runtime</scope>
   <optional>true</optional>
  </dependency>
  <dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <optional>true</optional>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.postgresql</groupId>
   <artifactId>postgresql</artifactId>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-jdbc</artifactId>
  </dependency>
 </dependencie

Implement the Code

I will show you how to create a record by connecting Spring Boot with PostgreSQL. I implemented the following code snippet as the main Java file—no need for additional controllers or other files put here.

package com.example.prostgre;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.beans.factory.annotation.Autowired;


@SpringBootApplication
public class ProstgreApplication implements CommandLineRunner {
 
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    public static void main(String[] args) {
        SpringApplication.run(ProstgreApplication.class, args);
    }
 
    @Override
    public void run(String... args) throws Exception {
        String sql = "INSERT INTO students (name, email) VALUES ("
                + "'Lahiru Ariyasinghe', '[email protected]')";
         
        int rows = jdbcTemplate.update(sql);
        if (rows > 0) {
            System.out.println("A new row has been inserted.");
        }
    }
 
}

Connect with PostgreSQL

Finally, I connect to the PostgreSQL by updating the application.properties file.

spring.datasource.url=jdbc:postgresql://localhost:5432/testme
spring.datasource.username=postgres
spring.datasource.password=1234

Before Running…

Before the end, we must create a new DB(testme) in pgAdmin 4 and create a new table called students. Students table has the following structure.

None

See the Results

When you run the main method in the ProstgreApplication, you can see one record in the table if your project successfully runs.

None

This is how you can connect your Spring Boot project with PostgreSQL.

Happy Coding !!!! Found this post useful? Kindly tap the 👏 button below! :)