Spring Boot with PostgreSQL: Connect a Real Database (Beginner Guide)
Connect Spring Boot to PostgreSQL step by step: add the driver, configure the datasource with env-var credentials, create the database, and persist data across restarts.
So far we've talked about databases in the abstract. Now let's get concrete and connect Spring Boot to PostgreSQL — a powerful, free, open-source database that's a favourite for production applications (it's exactly what this website runs on). By the end, you'll know how to wire up a real, persistent database that keeps your data safe even when the app restarts.
Why PostgreSQL?
When you're learning, Spring Boot often uses an in-memory database (H2) that's wiped every time the app stops — fine for experiments, useless for real data. PostgreSQL (or "Postgres") is a serious, durable database that:
- Stores data permanently on disk — survives restarts and crashes.
- Is free, open-source, and hugely popular in industry (great for your résumé).
- Handles everything from small apps to massive production systems.
Learning to connect a real database is a milestone — it's the difference between a toy and an app that remembers things.
Step 1: Add the PostgreSQL driver
Your app needs the Postgres JDBC driver — the library that lets Java talk to Postgres. Add it alongside Spring Data JPA in your build file:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Step 2: Configure the connection
Tell Spring Boot how to reach your database in application.yml. Notice the credentials come from environment variables — exactly the secret-safe pattern from the config lesson:
spring:
datasource:
url: jdbc:postgresql://localhost:5432/myapp_db
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
jpa:
hibernate:
ddl-auto: update # auto-manage the table schema
show-sql: true # log SQL (handy while learning)
url—jdbc:postgresql://HOST:PORT/DATABASE_NAME. Default Postgres port is 5432.ddl-auto: update— Hibernate creates/updates tables from your@Entityclasses automatically. Great for development.show-sql: true— prints the SQL Spring generates, so you can see exactly what's happening.
Step 3: Create the database
Spring can auto-manage tables, but you create the database itself once. Install PostgreSQL, then create your database (via the psql tool or a GUI like pgAdmin/DBeaver):
CREATE DATABASE myapp_db;
That's the only manual SQL you need to get started. Your entities become the tables.
Step 4: Run it — your data now persists!
Start your app. With show-sql on, you'll see Hibernate creating tables and running queries in the console. Save some data through your API, stop the app, restart it — and your data is still there. That's the magic of a real database: persistence across restarts. Congratulations, you now have a genuinely production-shaped stack.
ddl-auto in production: update is convenient for development, but in production teams usually switch to validate (check the schema matches, change nothing) and manage schema changes with a migration tool like Flyway or Liquibase. For learning, update is perfect — just know the grown-up approach exists for when you need tighter control.
The full stack, connected
You've now assembled every layer: a REST controller receives requests, a service holds logic, a JPA repository talks to PostgreSQL, and data persists for real. That's a complete, professional backend architecture — the same shape used by countless production systems.
Connecting my first app to PostgreSQL was a real milestone moment — watching data survive a restart made everything feel real in a way in-memory experiments never did. This website runs on precisely this setup: Spring Boot + Spring Data JPA + PostgreSQL, with credentials in environment variables and ddl-auto tuned per environment. My advice for your first Postgres connection: turn on show-sql so you can see the SQL Spring generates from your entities and repository methods — it's genuinely illuminating, and demystifies what JPA is doing under the hood. Once your data persists across restarts, you've built something real.
Key takeaways
- PostgreSQL is a durable, free, production-grade database — data survives restarts, unlike in-memory H2.
- Add the PostgreSQL driver + Spring Data JPA, then configure the
datasourceURL and env-var credentials. ddl-auto: updateauto-manages tables from entities in dev; usevalidate+ migrations in production.- Create the database once (
CREATE DATABASE); your entities become the tables automatically.
← Previous: Spring Lesson 11 — Testing
Next: Spring Lesson 13 — Actuator & Logging →
↑ Back to the Spring Boot roadmap