Spring Boot + PostgreSQL
Java Spring Boot application with PostgreSQL.
Overview
Spring Boot is an opinionated framework built on top of the Java Spring ecosystem that eliminates much of the boilerplate configuration traditionally required for enterprise Java applications. Launched by Pivotal in 2014, Spring Boot provides auto-configuration, embedded servers, and production-ready features out of the box, allowing developers to create stand-alone applications with minimal setup. It has become the de facto standard for modern Java microservices and web applications, offering convention-over-configuration principles that dramatically reduce development time.
This stack combines Spring Boot's rapid development capabilities with PostgreSQL's enterprise-grade relational database features. Spring Boot's built-in JPA/Hibernate integration automatically handles database connections, transaction management, and ORM mapping, while PostgreSQL provides ACID compliance, advanced indexing, and support for both relational and JSON data types. The configuration leverages Spring's externalized configuration system to inject database connection parameters through environment variables, enabling the application to connect to PostgreSQL without hardcoded credentials.
This combination is ideal for Java developers building data-driven applications who need both development velocity and production reliability. Startups can leverage Spring Boot's rapid prototyping capabilities alongside PostgreSQL's schema flexibility, while enterprises benefit from Spring's mature ecosystem and PostgreSQL's proven track record in mission-critical systems. The stack particularly excels for applications requiring complex business logic, transactional integrity, and the ability to handle both structured relational data and semi-structured JSON documents within the same database.
Key Features
- Spring Boot auto-configuration automatically detects PostgreSQL on the classpath and configures DataSource, JPA, and transaction management
- Hibernate DDL auto-generation creates and updates database schemas based on JPA entity annotations
- PostgreSQL JSONB column support through JPA @Column annotations for storing semi-structured data alongside relational tables
- Spring Data JPA repositories provide automatic CRUD operations and query method generation from method names
- PostgreSQL connection pooling via HikariCP (Spring Boot's default) optimizes database connection management
- Spring Boot Actuator health checks automatically monitor PostgreSQL connectivity at /actuator/health
- PostgreSQL full-text search capabilities accessible through native queries in Spring Data repositories
- Spring's @Transactional annotation leverages PostgreSQL's MVCC for concurrent transaction handling
Common Use Cases
- 1E-commerce platforms requiring inventory management, order processing, and financial transaction integrity
- 2Content management systems needing structured metadata storage with flexible JSON content fields
- 3Financial applications demanding ACID compliance for accounting, payment processing, and audit trails
- 4SaaS applications with multi-tenant data isolation using PostgreSQL row-level security features
- 5Analytics dashboards combining relational reporting with PostgreSQL's window functions and CTEs
- 6Geographic applications leveraging PostGIS extension with Spring Boot's REST APIs for location services
- 7Enterprise applications requiring complex business logic with JPA entity relationships and PostgreSQL foreign key constraints
Prerequisites
- Docker and Docker Compose installed with minimum 2GB available RAM for PostgreSQL and JVM heap space
- Java development knowledge including Maven/Gradle build tools for creating the Spring Boot application Dockerfile
- Port 8080 available for Spring Boot web server and port 5432 free for PostgreSQL connections
- Understanding of JPA/Hibernate entity mapping and Spring Data repository patterns
- Basic PostgreSQL knowledge for writing custom queries and understanding connection parameters
- Environment file (.env) configured with DB_NAME, DB_USER, and DB_PASSWORD variables
For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms
docker-compose.yml
docker-compose.yml
1services: 2 app: 3 build: .4 container_name: spring-boot5 environment: 6 SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/${DB_NAME}7 SPRING_DATASOURCE_USERNAME: ${DB_USER}8 SPRING_DATASOURCE_PASSWORD: ${DB_PASSWORD}9 ports: 10 - "8080:8080"11 depends_on: 12 - postgres13 networks: 14 - spring1516 postgres: 17 image: postgres:16-alpine18 container_name: postgres19 environment: 20 POSTGRES_DB: ${DB_NAME}21 POSTGRES_USER: ${DB_USER}22 POSTGRES_PASSWORD: ${DB_PASSWORD}23 volumes: 24 - postgres_data:/var/lib/postgresql/data25 networks: 26 - spring2728volumes: 29 postgres_data: 3031networks: 32 spring: 33 driver: bridge.env Template
.env
1DB_NAME=springboot2DB_USER=spring3DB_PASSWORD=changemeUsage Notes
- 1Docs: https://spring.io/projects/spring-boot
- 2Create multi-stage Dockerfile (maven/gradle build + JRE runtime)
- 3Access at http://localhost:8080
- 4Hibernate/JPA auto-manages schema (spring.jpa.hibernate.ddl-auto)
- 5Use Flyway or Liquibase for production migrations
- 6Actuator endpoints at /actuator for health checks
Individual Services(2 services)
Copy individual services to mix and match with your existing compose files.
app
app:
build: .
container_name: spring-boot
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/${DB_NAME}
SPRING_DATASOURCE_USERNAME: ${DB_USER}
SPRING_DATASOURCE_PASSWORD: ${DB_PASSWORD}
ports:
- "8080:8080"
depends_on:
- postgres
networks:
- spring
postgres
postgres:
image: postgres:16-alpine
container_name: postgres
environment:
POSTGRES_DB: ${DB_NAME}
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- spring
Quick Start
terminal
1# 1. Create the compose file2cat > docker-compose.yml << 'EOF'3services:4 app:5 build: .6 container_name: spring-boot7 environment:8 SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/${DB_NAME}9 SPRING_DATASOURCE_USERNAME: ${DB_USER}10 SPRING_DATASOURCE_PASSWORD: ${DB_PASSWORD}11 ports:12 - "8080:8080"13 depends_on:14 - postgres15 networks:16 - spring1718 postgres:19 image: postgres:16-alpine20 container_name: postgres21 environment:22 POSTGRES_DB: ${DB_NAME}23 POSTGRES_USER: ${DB_USER}24 POSTGRES_PASSWORD: ${DB_PASSWORD}25 volumes:26 - postgres_data:/var/lib/postgresql/data27 networks:28 - spring2930volumes:31 postgres_data:3233networks:34 spring:35 driver: bridge36EOF3738# 2. Create the .env file39cat > .env << 'EOF'40DB_NAME=springboot41DB_USER=spring42DB_PASSWORD=changeme43EOF4445# 3. Start the services46docker compose up -d4748# 4. View logs49docker compose logs -fOne-Liner
Run this command to download and set up the recipe in one step:
terminal
1curl -fsSL https://docker.recipes/api/recipes/spring-boot-postgres/run | bashTroubleshooting
- Application fails with 'Failed to configure a DataSource' error: Ensure PostgreSQL environment variables match between app and postgres services, and verify depends_on is properly configured
- PostgreSQL connection refused on startup: Check that both services are on the same Docker network and PostgreSQL container is fully initialized before Spring Boot attempts connection
- Hibernate schema validation errors: Set spring.jpa.hibernate.ddl-auto=create-drop for development or use Flyway/Liquibase for production schema management
- Out of memory errors in Spring Boot container: Increase Docker memory limits and configure JVM heap size using JAVA_OPTS environment variable
- PostgreSQL data lost after container restart: Verify postgres_data volume is properly mounted to /var/lib/postgresql/data in the compose configuration
- Slow query performance: Enable PostgreSQL query logging with log_statement=all and analyze with EXPLAIN ANALYZE, then add appropriate database indexes
Community Notes
Loading...
Loading notes...
Download Recipe Kit
Get all files in a ready-to-deploy package
Includes docker-compose.yml, .env template, README, and license
Ad Space
Shortcuts: C CopyF FavoriteD Download