How to Build CRUD Application Using Spring Boot and PostgreSQL

CRUD apps with spring boot

Building a CRUD (Create, Read, Update, Delete) application is one of the most common tasks in modern backend development. In 2025, Spring Boot and PostgreSQL remain a powerful and reliable combination for developing scalable, production-ready applications.

This guide walks you step by step through building a simple but complete CRUD application using Spring Boot and PostgreSQL, following best practices for clean code, security, and maintainability.

What Is a CRUD Application?

CRUD stands for:

  • Create – add new data
  • Read – retrieve existing data
  • Update – modify stored data
  • Delete – remove data

Most business applications—such as customer management systems, inventory platforms, and admin dashboards—are built around CRUD operations.

Why Use Spring Boot and PostgreSQL?

Spring Boot Advantages

  • Fast project setup
  • Opinionated configuration
  • Built-in dependency management
  • Excellent ecosystem (Spring Data JPA, Spring Security)

PostgreSQL Advantages

  • Open-source and highly reliable
  • Advanced SQL features
  • Strong data integrity
  • Excellent performance for transactional workloads

Together, they form a solid foundation for enterprise and startup applications alike.

Prerequisites

Before you start, make sure you have:

  • Java JDK 17 or later
  • Maven or Gradle
  • PostgreSQL installed and running
  • Basic understanding of Java and REST APIs

Step 1: Create a Spring Boot Project

Use Spring Initializr with the following dependencies:

  • Spring Web
  • Spring Data JPA
  • PostgreSQL Driver
  • Lombok (optional)

Project structure:

src/main/java
 └── com.example.crudapp
     ├── controller
     ├── service
     ├── repository
     └── model

Step 2: Configure PostgreSQL Database

Create a database in PostgreSQL:

CREATE DATABASE crud_db;

Update application.yml:

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/crud_db
    username: postgres
    password: yourpassword
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    properties:
      hibernate:
        format_sql: true

Step 3: Create the Entity Model

Example Customer entity:

@Entity
@Table(name = "customers")
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private String email;
    private String phone;
}

This entity will automatically map to a PostgreSQL table.

Step 4: Create the Repository Layer

Use Spring Data JPA:

public interface CustomerRepository extends JpaRepository<Customer, Long> {
}

Spring Boot automatically generates common CRUD queries.

Step 5: Implement the Service Layer

The service layer handles business logic:

@Service
public class CustomerService {

    private final CustomerRepository repository;

    public CustomerService(CustomerRepository repository) {
        this.repository = repository;
    }

    public List<Customer> findAll() {
        return repository.findAll();
    }

    public Customer save(Customer customer) {
        return repository.save(customer);
    }

    public void deleteById(Long id) {
        repository.deleteById(id);
    }
}

Step 6: Create REST Controller

Expose REST APIs:

@RestController
@RequestMapping("/api/customers")
public class CustomerController {

    private final CustomerService service;

    public CustomerController(CustomerService service) {
        this.service = service;
    }

    @GetMapping
    public List<Customer> getAll() {
        return service.findAll();
    }

    @PostMapping
    public Customer create(@RequestBody Customer customer) {
        return service.save(customer);
    }

    @PutMapping("/{id}")
    public Customer update(@PathVariable Long id, @RequestBody Customer customer) {
        customer.setId(id);
        return service.save(customer);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        service.deleteById(id);
    }
}

Step 7: Test CRUD APIs

Use Postman or cURL:

curl -X GET http://localhost:8080/api/customers

Verify that all CRUD operations work correctly.

Best Practices for Spring Boot CRUD Applications

Use DTOs

Avoid exposing entities directly to the API.

Validate Input

Use @Valid and Bean Validation.

Handle Exceptions Globally

Implement @ControllerAdvice.

Secure Endpoints

Add Spring Security and role-based access control.

Common Mistakes to Avoid

  • Using entity classes directly as API responses
  • Skipping input validation
  • Hardcoding database credentials
  • Ignoring pagination for large datasets
  • Not handling exceptions properly

Performance Optimization Tips

  • Enable pagination and sorting
  • Use indexes in PostgreSQL
  • Avoid unnecessary eager fetching
  • Use connection pooling (HikariCP)

When to Use This Architecture

This Spring Boot CRUD setup is ideal for:

  • Admin dashboards
  • Microservices
  • REST APIs
  • Internal enterprise applications

Conclusion

Learning how to build a CRUD application using Spring Boot and PostgreSQL is a foundational skill for backend developers. With clean architecture, proper configuration, and best practices, you can build scalable, maintainable, and production-ready applications.

Spring Boot simplifies development, while PostgreSQL ensures reliability and performance—making them a perfect match in 2025.

(Visited 17 times, 1 visits today)

You may also like