Skip to content
Related Articles
Open in App
Not now

Related Articles

Spring Boot Integration With PostgreSQL as a Maven Project

Improve Article
Save Article
  • Last Updated : 09 Oct, 2022
Improve Article
Save Article

PostgreSQL is a user-friendly versatile RDBMS. This article lets us see how to integrate Spring Data JPA with PostgreSQL. There are some conventions to be followed while using PostgreSQL. We will cover that also.

Working with PostgreSQL

We can easily create databases and tables in that. The below screenshot will help with how to connect to PostgreSQL via the command line


Now let us create a table named “geek_author” under “geeksforgeeks” database.

Note: During table creation, PostgreSQL expects _ as the separator between two words.


Sequence is necessary as we are using “GenerationType.SEQUENCE”. Hence we need to create by using

CREATE SEQUENCE hibernate_sequence START 1;

Example Project

Project Structure:

Project Structure


This is the maven project. All dependencies need to be specified under pom.xml. As we are connecting PostgreSQL, this dependency is significant


As we are using spring data JPA, this dependency is essential




<?xml version="1.0" encoding="UTF-8"?>
    <description>Demo project for Spring Boot</description>
        <relativePath />
        <!--postgresql dependency -->

Let’s start with the Model class. Namingwise let it be concurrent with the Postgres table


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name = "geek_author")
public class geek_author {
    private Integer author_id;
    private String author_name;
    private String email_id;
    public Integer getAuthor_id() {
        return author_id;
    public void setAuthor_id(Integer author_id) {
        this.author_id = author_id;
    public String getAuthor_name() {
        return author_name;
    public void setAuthor_name(String author_name) {
        this.author_name = author_name;
    public String getEmail_id() {
        return email_id;
    public void setEmail_id(String email_id) {
        this.email_id = email_id;

Let us write the repository class now


import org.springframework.stereotype.Repository;
import com.gfg.postgresql.model.geek_author;
public interface AuthorRepository extends JpaRepository<geek_author, Long>{


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gfg.postgresql.model.geek_author;
import com.gfg.postgresql.repository.AuthorRepository;
// This means that this 
// class is a Controller
// This means URL's start with /geek (after Application path)
public class AuthorController {
    // This means to get the bean called geekuserRepository
    // Which is auto-generated by Spring, we will use it
    // to handle the data
    private AuthorRepository authorRepository;
    // Map ONLY POST Requests
    public @ResponseBody String addAuthors (@RequestParam String author_name
            , @RequestParam String email_id) {
        // @ResponseBody means the returned String
        // is the response, not a view name
        // @RequestParam means it is a parameter
        // from the GET or POST request      
        geek_author geekAuthor = new geek_author();
        return "Details got Saved";
    public @ResponseBody Iterable<geek_author> getAllAuthors() {
        // This returns a JSON or 
        // XML with the Book
        return authorRepository.findAll();

Application that can be started is coded as follows :


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class AuthorApplication {
    public static void main(String[] args) {, args);

When everything is correct, easily this can be run as a normal Java application


Console Output:

Console Output


Adding authors: This is done via the POST method in the controller

Let’s use the “postman” client to add authors

use the "postman" client to add authors


Retrieving Authors:

We can easily retrieve the author’s details as follows:



As we have inserted 3 records already, we are getting all the author details in the above screenshot.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!