PostgreSQL is a production-grade relational database system widely used across modern enterprise and microservice architectures. If you’re building a Spring Boot application, connecting it to PostgreSQL isn’t just a matter of adding a dependency—it’s about configuring it the right way from day one.
This guide walks you through how to connect a Spring Boot application to PostgreSQL using Spring Data JPA, initialize the schema reliably and understand the pros and cons of using ddl-auto
.
📦 Step 1: Add the Required Dependencies
For any JPA-backed integration with PostgreSQL, you need two core dependencies:
<!-- Spring Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- PostgreSQL JDBC Driver -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.3</version>
</dependency>
If you’re using Gradle:
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.postgresql:postgresql:42.7.3'
spring-boot-starter-data-jpa
brings in Hibernate as the default JPA provider, and abstracts common repository patterns with ease using JpaRepository
.
⚙️ Step 2: Configure Your Connection
In src/main/resources/application.properties
, configure your datasource and JPA settings:
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=myuser
spring.datasource.password=mypassword
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
🛡️ Warning:
ddl-auto=create-drop
may seem convenient, but it can lead to accidental schema drift in production. You should useddl-auto=none
and manage your schema manually or via a migration tool.
🔍 ddl-auto
explained
Spring Data JPA offers some options for the ddl-auto
property:
Value | Behavior |
---|---|
create | Drops and re-creates schema on startup |
create-drop | Same as create , but also drops on shutdown |
update | Tries to automatically alter the schema |
validate | Verifies schema matches entity mappings |
none | No action; you manage the schema |
Recommended Use:
✔️ Good for quick prototyping in dev
❌ Not for production or long-term evolution
For Production environment, I strongly suggest going with a high-level migration tool like Flyway and Liquibase or going with a manual and separated approach.
Trust me, it is really easy to make a mess by using
ddl-auto
.
🛠️ Step 3: Run PostgreSQL Locally (with Docker)
docker run --name my-postgres \
-e POSTGRES_USER=myuser \
-e POSTGRES_PASSWORD=mypassword \
-e POSTGRES_DB=mydb \
-p 5432:5432 -d postgres:16
This creates a container with PostgreSQL running on port 5432
.
🧪 Step 4: Build a Sample Entity and Repository
User.java:
@Entity
@Table(name = "orders")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String description;
// Getters and setters
}
UserRepository.java:
public interface OrderRepository extends JpaRepository<Order, Long> {}
UserController.java:
@RestController
@RequestMapping("/orders")
public class OrderController {
private final OrderRepository repository;
public OrderController(OrderRepository repository) {
this.repository = repository;
}
@GetMapping
public List<Order> findAll() {
return repository.findAll();
}
@PostMapping
public Order create(@RequestBody Order order) {
return repository.save(order);
}
}
You now have a working API backed by PostgreSQL and powered by Spring Data JPA.
You can test your app by running the following cURL
commands:
# Creates an Order
curl -X POST <http://localhost:8080/orders> \\
-H "Content-Type: application/json" \\
-d '{"description": "Order 1"}'
# {"id":1,"description":"Order 1"}
# List Orders
curl <http://localhost:8080/orders>
# [{"id":1,"description":"Order 1"}]

📦 Bonus: Is Flyway Better?
Yes — for larger, evolving applications, consider using Flyway for version-controlled migrations. But for smaller apps or bootstrapping ddl-auto
work just fine.
✅ Conclusion
Connecting your Spring Boot application to PostgreSQL is simple—but doing it right takes understanding. By using Spring Data JPA, managing schema explicitly, and avoiding dangerous auto-DDL behaviors in production, you ensure your backend is reliable, maintainable, and production-ready.
📘 Want More?
Download my free eBook:
👉 Mastering Spring Boot Project Structure – Structure Spring Projects Like a Pro
And watch my full tutorials on YouTube: