Spring Boot is a popular framework for building modern applications, and one of its key features is the ability to easily connect to a variety of databases. In this tutorial, we’ll show you how to connect a PostgreSQL database to your Spring Boot application.

Setting up the PostgreSQL Database

Before we can connect to the database, we need to set it up. If you don’t already have a PostgreSQL database, you can download and install one from the PostgreSQL website.

Next, create a new database for your Spring Boot application. You can do this by running the following command in the PostgreSQL terminal:

CREATE DATABASE your_database_name;

Once you’ve created the database, you’ll need to create a user and password for the Spring Boot application to connect with. You can do this by running the following commands:

CREATE USER your_user_name WITH PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE your_database_name TO your_user_name;

Configuring the Spring Boot Application

With the database set up, we can now move on to configuring the Spring Boot application.

First, you’ll need to add the following dependencies to your pom.xml file:



Next, add the following properties to your application.properties file:


These properties configure the Spring Boot application to connect to the PostgreSQL database you created earlier.

Creating a Model Class

With the database configuration out of the way, we can now move on to creating the model class for our data. A model class represents the structure of the data we’ll be storing in the database.

Here’s an example of a simple model class for a Person:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

public class Person {

   @GeneratedValue(strategy = GenerationType.AUTO)
   private long id;

   private String firstName;
   private String lastName;

   // getters and setters

Note the use of the @Entity annotation, which tells Spring that this class represents a database entity. The @Id and @GeneratedValue annotations are used to specify the primary key and the strategy for generating its values.

Creating a Repository

With the model class in place, we can now create a repository to interact with the database. A repository is an interface that extends Spring’s JpaRepository and defines the methods for querying and manipulating data in the database.

Here’s an example of a PersonRepository:

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

public interface PersonRepository extends JpaRepository<Person, Long> {

Note the use of the @Repository annotation, which tells Spring that this is a repository. The JpaRepository takes two generic arguments: the type of the entity it will be managing and the type of the primary key.

Creating a Service

With the repository in place, we can now create a service to handle business logic. A service is a class that interacts with the repository to perform operations on the data.

Here’s an example of a PersonService:

import org.springframework.stereotype.Service;

public class PersonService {

   private final PersonRepository personRepository;

   public PersonService(PersonRepository personRepository) {
      this.personRepository = personRepository;

   public List<Person> findAll() {
      return personRepository.findAll();

   public Person save(Person person) {
      return personRepository.save(person);

Note the use of the @Service annotation, which tells Spring that this is a service. The service uses the PersonRepository to perform operations on the data.

Testing the Connection

With everything in place, we can now test the connection to the PostgreSQL database.

Create a new PersonController and add the following code:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

public class PersonController {

   private final PersonService personService;

   public PersonController(PersonService personService) {
      this.personService = personService;

   public List<Person> findAll() {
      return personService.findAll();

   public Person save(@RequestBody Person person) {
      return personService.save(person);

This controller uses the PersonService to perform operations on the data and exposes the functionality through REST endpoints.

Now, run the Spring Boot application and test the endpoints using a tool like Postman. You should be able to save and retrieve data from the PostgreSQL database.


In this tutorial, we’ve shown you how to connect a PostgreSQL database to a Spring Boot application. We’ve covered setting up the database, configuring the Spring Boot application, creating a model class, a repository, and a service, and finally testing the connection. With this knowledge, you should be able to easily connect to any database of your choice and start building your application.

Leave a Reply