Connecting a
Spring Boot application to a
PostgreSQL database is one of the most common tasks in Java development. To make this connection clean, maintainable, and environment-specific, it’s best to use the
application.properties
file and Spring Boot's built-in
profiles feature.
In this guide, we’ll walk through how to configure
Spring Boot with PostgreSQL using
application.properties
for both
development and
production environments. You’ll also see how to manage dependencies and how to switch between environments effortlessly.
Let’s dive in!
What You’ll Learn
- How to organize your Spring Boot config using
application.properties
- How to set up PostgreSQL connections for dev and prod
- How to use Spring profiles to separate environments
- What dependencies you need to work with PostgreSQL
Step 1: Add PostgreSQL Dependency
First things first—your Spring Boot project needs the PostgreSQL driver to connect to a PostgreSQL database.
For 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>
For Gradle
Add this to your
build.gradle
:
implementation 'org.postgresql:postgresql:42.6.0'
Spring Boot will automatically pick up this dependency and use it as the default database driver when PostgreSQL is configured.
Step 2: Organize Configuration Files
Use Spring Boot’s
profile-based configuration system to separate dev and prod environments.
application.properties
: Base file (used to define the active profile)
application-dev.properties
: Settings for development
application-prod.properties
: Settings for production
This allows you to write different configs without changing code when switching environments.
Step 3: Configure Development Environment
Here's a simple
application-dev.properties
file for local development:
# Activate development profile
spring.datasource.url=jdbc:postgresql://localhost:5432/devdb
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driver-class-name=org.postgresql.Driver
# JPA settings for development
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
In this example:
- We're connecting to a local PostgreSQL database.
ddl-auto=update
allows Hibernate to automatically adjust the schema.
show-sql=true
helps with debugging by printing SQL statements to the console.
Step 4: Configure Production Environment
In production, you should
never hardcode sensitive information. Instead, use
environment variables.
Here’s what
application-prod.properties
might look like:
# PostgreSQL - Production
spring.datasource.url=${DB_URL}
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}
spring.datasource.driver-class-name=org.postgresql.Driver
# JPA settings for production
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.show-sql=false
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
Example environment variables:
DB_URL=jdbc:postgresql://prod-db-host:5432/proddb
DB_USERNAME=produser
DB_PASSWORD=securepass
Tip: Use ddl-auto=validate
in production to make sure your schema is correct but never auto-updated.
Step 5: Set Active Profile
You can specify the active profile in your base
application.properties
:
spring.profiles.active=dev
Or override it at runtime:
java -jar yourapp.jar --spring.profiles.active=prod
Or with an environment variable:
export SPRING_PROFILES_ACTIVE=prod
This tells Spring Boot which
.properties
file to load—either
application-dev.properties
or
application-prod.properties
.
Step 6: Verify the Setup
Once everything is configured:
- Start your PostgreSQL server (locally or remotely).
- Run your Spring Boot app.
- Check the logs to confirm connection success.
- If there’s an error (e.g.,
org.postgresql.util.PSQLException
), check your credentials and DB host.
Best Practices
- Use
application-dev.properties
for local development.
- Avoid committing sensitive data to version control.
- Use
spring.profiles.active
to toggle environments.
- Use
validate
or none
for ddl-auto
in production to avoid unintentional schema changes.
- Always test your configuration locally before deploying.
Troubleshooting Tips
Problem |
Solution |
App fails to connect to DB |
Check DB URL, port, and credentials |
“Driver not found” error |
Make sure PostgreSQL dependency is added |
Schema not updating |
Make sure ddl-auto=update is set (for dev only) |
Password showing in logs |
Set spring.jpa.show-sql=false and avoid logging sensitive info |
image quote pre code