When you're building Java applications with Quarkus, managing environment-specific configurations is crucial—especially when connecting to services like PostgreSQL. Thanks to Quarkus’s support for profiles and YAML-based configuration, it’s easy to set up clean, isolated environments for development and production.
In this guide, we’ll walk through configuring PostgreSQL in a Quarkus application using Docker for local development and Kubernetes for production. We’ll organize our configuration using
application-dev.yml
and
application-prod.yml
so that switching between environments is smooth and low-maintenance.
1. Add the Necessary Dependencies
First, make sure your
pom.xml
includes the right dependencies:
<dependencies>
<!-- Quarkus JDBC support for PostgreSQL -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
<!-- Hibernate ORM for JPA support -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm</artifactId>
</dependency>
<!-- YAML configuration support -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-config-yaml</artifactId>
</dependency>
<!-- Kubernetes configuration (for prod) -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-kubernetes-config</artifactId>
</dependency>
</dependencies>
These enable PostgreSQL support, JPA integration, YAML-based configuration, and Kubernetes ConfigMap/Secret loading.
2. Start PostgreSQL Using Docker (Development)
Here’s a simple command to start a PostgreSQL container for development:
docker run --name postgres-dev \
-e POSTGRES_USER=devuser \
-e POSTGRES_PASSWORD=devpass \
-e POSTGRES_DB=devdb \
-p 5432:5432 \
-d postgres:14
This creates a local PostgreSQL instance with the database
devdb
, running on port
5432
.
3. Configure application-dev.yml
Now let’s define development-specific settings in
src/main/resources/application-dev.yml
:
"%dev":
quarkus:
datasource:
db-kind: postgresql
jdbc:
url: jdbc:postgresql://localhost:5432/devdb
driver: org.postgresql.Driver
username: devuser
password: devpass
hibernate-orm:
database:
generation: drop-and-create
dialect: org.hibernate.dialect.PostgreSQLDialect
This setup ensures your dev environment uses Docker-hosted PostgreSQL and automatically regenerates the schema on app startup.
Run the app with:
./mvnw quarkus:dev -Dquarkus.profile=dev
4. Production Setup Using Kubernetes
For production, you should avoid hardcoded values. Kubernetes allows you to manage configurations using ConfigMaps and Secrets.
4.1: Create a ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: postgres-config
data:
db-kind: postgresql
jdbc-url: jdbc:postgresql://postgres-service:5432/proddb
hibernate-dialect: org.hibernate.dialect.PostgreSQLDialect
4.2: Create a Secret
apiVersion: v1
kind: Secret
metadata:
name: postgres-secret
type: Opaque
data:
username: cHJvZHVzZXI= # base64 for 'produser'
password: cHJvZHBhc3M= # base64 for 'prodpass'
5. Configure application-prod.yml
In your
src/main/resources/application-prod.yml
, refer to values through environment variables (these will be injected by Kubernetes):
"%prod":
quarkus:
datasource:
db-kind: ${DB_KIND:postgresql}
jdbc:
url: ${JDBC_URL}
driver: org.postgresql.Driver
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
hibernate-orm:
database:
generation: validate
dialect: ${HIBERNATE_DIALECT}
kubernetes-config:
enabled: true
config-maps: postgres-config
secrets: postgres-secret
This file instructs Quarkus to dynamically load its config from the Kubernetes environment.
6. Configure Kubernetes Deployment
In your Quarkus deployment file, make sure to set up environment variables from the ConfigMap and Secret:
env:
- name: QUARKUS_PROFILE
value: prod
- name: DB_KIND
valueFrom:
configMapKeyRef:
name: postgres-config
key: db-kind
- name: JDBC_URL
valueFrom:
configMapKeyRef:
name: postgres-config
key: jdbc-url
- name: HIBERNATE_DIALECT
valueFrom:
configMapKeyRef:
name: postgres-config
key: hibernate-dialect
- name: DB_USERNAME
valueFrom:
secretKeyRef:
name: postgres-secret
key: username
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: postgres-secret
key: password
This setup allows your Quarkus app to securely connect to PostgreSQL in Kubernetes without hardcoding credentials.
By using
application.yml
profiles and separating your dev and prod configurations, you get the best of both worlds: fast and flexible development with Docker, and secure, scalable production deployment via Kubernetes.
Quarkus makes it easy to manage environment-specific behavior with its
%profile
syntax. Combine this with Kubernetes' configuration management tools, and your PostgreSQL integration stays clean, portable, and maintainable. whether you’re running locally or in the cloud.
image quote pre code