Skip to content

Why PostgreSQL

Hub › Java › Advanced › Why PostgreSQL

Goal

Understand why H2 is unsuitable for production and how to configure Spring Boot for PostgreSQL.

Prerequisites

  • Intermediate tier (JPA CRUD with H2)

H2 vs PostgreSQL

FeatureH2PostgreSQL
PersistenceIn-memory by defaultDisk-backed, WAL
ConcurrencySingle-connectionMulti-connection, MVCC
Data safetyLost on restartDurable (fsync, WAL)
Network accessEmbedded onlyTCP/IP, connection pooling
Production useNoYes

Spring Data JPA abstracts the database vendor, so switching from H2 to PostgreSQL requires only a dependency change and new connection properties.

Add PostgreSQL dependency

xml
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>

application-postgres.properties

Create src/main/resources/application-postgres.properties:

properties
spring.datasource.url=jdbc:postgresql://localhost:5432/itemsdb
spring.datasource.username=postgres
spring.datasource.password=secret
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update

Activate the profile

bash
# Start a local Postgres:
docker run -d --name pg \
  -e POSTGRES_DB=itemsdb \
  -e POSTGRES_PASSWORD=secret \
  -p 5432:5432 \
  postgres:17-alpine

# Run with the postgres profile:
./mvnw spring-boot:run -Dspring-boot.run.profiles=postgres

Checkpoint

bash
curl http://localhost:8080/items
# → []  (empty list, now backed by PostgreSQL)

docker stop pg && docker rm pg  # cleanup

Next: Flyway migrations — versioned schema management.