When you're building a Spring Boot application, connecting to a
PostgreSQL database is one of the most common and important steps. Whether you're running the app locally during development or deploying it to a live production server, managing your configuration cleanly is essential.
A great way to organize your database settings is by using the
application.yml
file. With Spring Boot’s built-in profile support, you can maintain different configurations for
development (dev) and
production (prod) environments in the same file.
In this article, you'll learn how to:
- Configure PostgreSQL for Spring Boot using
application.yml
- Set up dev and prod environments using Spring profiles
- Include the PostgreSQL dependency in your project
Let’s walk through it step-by-step.
1. Add PostgreSQL Dependency
Before configuring anything, make sure your project includes the PostgreSQL driver.
If you're using
Maven, add this to your
pom.xml
:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.6.0</version> <!-- Use the latest stable version -->
</dependency>
If you're using
Gradle, add this to your
build.gradle
:
implementation 'org.postgresql:postgresql:42.6.0'
This dependency allows Spring Boot to connect to your PostgreSQL database.
2. Structure of application.yml
with Profiles
Spring Boot allows you to define different sections within
application.yml
for specific environments by using
profiles.
Here’s the basic structure:
spring:
profiles:
active: dev
Then, you can separate the dev and prod configurations using
---
:
# Development configuration
spring:
profiles: dev
# dev settings here
---
# Production configuration
spring:
profiles: prod
# prod settings here
Let’s fill in the details.
3. Configure PostgreSQL for Development
Here’s an example configuration for local development:
spring:
profiles: dev
datasource:
url: jdbc:postgresql://localhost:5432/devdb
username: postgres
password: postgres
driver-class-name: org.postgresql.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
database-platform: org.hibernate.dialect.PostgreSQLDialect
What each line means:
url
: connection string to your local PostgreSQL database
ddl-auto: update
: automatically updates your database schema (handy in dev)
show-sql: true
: prints SQL statements in the console for debugging
4. Configure PostgreSQL for Production
For production, you’ll want tighter control over the schema and no logging of SQL. It’s also best to use environment variables to keep credentials secure.
spring:
profiles: prod
datasource:
url: ${DB_URL}
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
driver-class-name: org.postgresql.Driver
jpa:
hibernate:
ddl-auto: validate
show-sql: false
database-platform: org.hibernate.dialect.PostgreSQLDialect
Best practices:
- Use
ddl-auto: validate
or none
in production to avoid accidental schema changes.
- Hide credentials using environment variables (
DB_URL
, DB_USERNAME
, DB_PASSWORD
).
- Make sure environment variables are configured in your server or deployment system.
5. Complete Example of application.yml
Here’s how your final
application.yml
might look:
spring:
profiles:
active: dev
---
spring:
profiles: dev
datasource:
url: jdbc:postgresql://localhost:5432/devdb
username: postgres
password: postgres
driver-class-name: org.postgresql.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
database-platform: org.hibernate.dialect.PostgreSQLDialect
---
spring:
profiles: prod
datasource:
url: ${DB_URL}
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
driver-class-name: org.postgresql.Driver
jpa:
hibernate:
ddl-auto: validate
show-sql: false
database-platform: org.hibernate.dialect.PostgreSQLDialect
This setup allows your app to automatically use the correct configuration based on the active profile.
6. Switching Between Dev and Prod
You can change the active profile in several ways:
Option 1: In application.yml
spring:
profiles:
active: prod
Option 2: Command line
java -jar app.jar --spring.profiles.active=prod
Option 3: Environment variable
export SPRING_PROFILES_ACTIVE=prod
This flexibility lets you deploy the same codebase across different environments without making code changes.
Best Practices Summary
- Use Spring profiles to separate dev and prod logic
- Keep your credentials secure in production by using environment variables
- Use
ddl-auto: update
in development, but never in production
- Always test your production configuration before deploying live
- Use clear, consistent naming for databases in each environment
image quote pre code