
PostgreSQL is one of the most powerful and popular open-source relational databases available today. Whether you’re building a CRUD API, a SaaS platform, or a microservice, connecting Spring Boot to PostgreSQL is a common and essential skill for backend Java developers. If you’re looking for a practical Spring Boot PostgreSQL example, you’re in the right place.
In this guide, you’ll learn how to connect a Spring Boot 3 application to PostgreSQL, set up your configuration, define your models, and perform basic CRUD operations using Spring Data JPA — all using best practices in 2025.
🧱 Prerequisites
- Java 17 or higher
- Spring Boot 3.x
- PostgreSQL installed and running locally or in the cloud (e.g., Docker, pgAdmin, or Supabase)
- IDE (IntelliJ IDEA, VSCode, etc.)
⚙️ Step 1: Create a New Spring Boot Project
Use Spring Initializr with the following setup:
- Dependencies:
- Spring Web
- Spring Data JPA
- PostgreSQL Driver
- Spring Boot DevTools
Then click “Generate” to download your project and open it in your IDE.
🧾 Step 2: Configure PostgreSQL in application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/demo_db
spring.datasource.username=postgres
spring.datasource.password=your_password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
🔐 Tip: For production apps, store credentials securely using environment variables or Spring Cloud Config.
📦 Step 3: Create an Entity
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Double price;
// Getters and setters omitted for brevity
}
💾 Step 4: Create a Repository
public interface ProductRepository extends JpaRepository {
}
Spring Data JPA provides all the basic CRUD methods out-of-the-box — no need to implement them manually.
🎯 Step 5: Create a REST Controller
@RestController
@RequestMapping("/api/products")
public class ProductController {
@Autowired
private ProductRepository productRepository;
@GetMapping
public List getAll() {
return productRepository.findAll();
}
@PostMapping
public Product create(@RequestBody Product product) {
return productRepository.save(product);
}
@GetMapping("/{id}")
public Product getById(@PathVariable Long id) {
return productRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
}
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id) {
productRepository.deleteById(id);
}
}
✅ You now have a fully functional CRUD API connected to PostgreSQL.
🧪 Test It with Postman
- POST
/api/products
{
"name": "MacBook Air M3",
"price": 1299.99
}
- GET
/api/products
Returns all products - GET
/api/products/{id}
Returns a single product - DELETE
/api/products/{id}
Deletes a product
🐘 PostgreSQL Setup Tips
- Use tools like pgAdmin, TablePlus, or DBeaver for easy DB management
- Use Docker for consistent local development:
docker run --name pgdb -e POSTGRES_PASSWORD=your_password -p 5432:5432 -d postgres
- Use schemas and constraints (
@Column(nullable = false)
or@UniqueConstraint
) for production-grade models
📘 Related:
🧠 Final Thoughts
Spring Boot and PostgreSQL work seamlessly together — especially when paired with Spring Data JPA, which removes the boilerplate of database interaction. This setup is perfect for REST APIs, internal tools, and full-stack apps needing robust relational data.
Now that you’re connected, you can explore:
- ✅ Adding validation with
@Valid
and@Validated
- ✅ Advanced querying with
@Query
or Specification API - ✅ Security with Spring Security + JWT
- ✅ Containerizing your app with Docker