Overview
Argos Mesh uses PostgreSQL as the primary relational database for the Orders service. The database stores product inventory, pricing, and stock information.PostgreSQL Configuration
Docker Setup
The database runs as a PostgreSQL 15 container:docker-compose.yml
Environment Variables
Database superuser usernameDefault:
user_shopDocker: Set via environment variableDatabase superuser passwordDefault:
secretPasswordInitial database nameDefault:
shop_dbSpring Datasource Configuration
Connection Settings
JDBC connection URLDefault:
jdbc:postgresql://db:5432/shop_dbFormat: jdbc:postgresql://{host}:{port}/{database}Local Development: jdbc:postgresql://localhost:5432/shop_dbDatabase authentication usernameDefault:
user_shopDatabase authentication passwordDefault:
secretPasswordJDBC driver classDefault:
org.postgresql.DriverApplication Properties
JPA/Hibernate Configuration
Schema Management
Hibernate schema generation strategyDefault:
updateOptions:none- No schema managementvalidate- Validate schema, make no changesupdate- Update schema if neededcreate- Create schema, destroying existing datacreate-drop- Create schema, drop on shutdown
Hibernate SQL dialectDefault:
org.hibernate.dialect.PostgreSQLDialectPurpose: Enables PostgreSQL-specific optimizations and SQL featuresDefer initialization until after entity scanningDefault:
truePurpose: Ensures Hibernate creates tables before data.sql runsSQL initialization modeDefault:
alwaysOptions:always- Always execute data.sqlembedded- Only for embedded databasesnever- Disable SQL initialization
Database Schema
Products Table
The core table for product inventory management:Entity Mapping
Product.java
Column Descriptions
Primary key with auto-incrementStrategy:
IDENTITY (PostgreSQL SERIAL)Java Type: LongProduct display nameConstraints: NOT NULL, NOT BLANKExample:
"Remera Oversize Black"Product price with 2 decimal placesConstraints: NOT NULL, must be positiveJava Type:
BigDecimalExample: 15900.00Available inventory quantityConstraints: NOT NULL, must be >= 0Java Type:
IntegerExample: 100Initial Data Loading
The system loads seed data fromdata.sql on startup:
data.sql
Idempotency: This script runs on every startup. In production, use Flyway or Liquibase for versioned migrations.
Connection Pool Configuration
Spring Boot uses HikariCP as the default connection pool (no configuration needed for basic usage).Advanced Tuning (Optional)
application.properties
Connection Pool Best Practices
Connection Pool Best Practices
Maximum Pool Size:
- Formula:
(CPU cores * 2) + effective spindle count - For most applications: 10-20 connections is sufficient
- More connections != better performance
- Keep 5-10 connections ready for burst traffic
- Reduces latency on sudden load increases
- 30 seconds is reasonable for most applications
- Increase if database is slow to respond
- 10 minutes (600000ms) allows reuse during quiet periods
- Prevents holding unnecessary connections
Health Checks
Docker Health Check
PostgreSQL container usespg_isready for health verification:
docker-compose.yml
Application Health Check
Spring Boot Actuator automatically exposes database health:Database Operations
Connect to Database
Common SQL Queries
Backup and Recovery
Data Persistence
PostgreSQL data is stored in a Docker volume:docker-compose.yml
- Data survives container restarts
- Data survives
docker-compose down - Data is lost only with
docker-compose down -v(volume removal)
Backup Commands
Troubleshooting
Connection Refused
Connection Refused
Symptoms: Application can’t connect to databaseSolutions:
- Verify PostgreSQL is running:
docker ps | grep shop_db - Check logs:
docker logs shop_db - Verify health check:
docker inspect shop_db | grep Health - Ensure database is ready before app starts (use
depends_onwithcondition: service_healthy)
Schema Not Updated
Schema Not Updated
Symptoms: New entity fields don’t appear in databaseSolutions:
- Verify
spring.jpa.hibernate.ddl-auto=update - Check for Hibernate errors in logs
- Manually create columns if needed
- Consider using Flyway/Liquibase for production
Data Initialization Fails
Data Initialization Fails
Symptoms:
data.sql doesn’t execute or failsSolutions:- Ensure
spring.sql.init.mode=always - Set
spring.jpa.defer-datasource-initialization=true - Check for SQL syntax errors
- Verify table exists before INSERT
Connection Pool Exhausted
Connection Pool Exhausted
Symptoms:
HikariPool - Connection is not availableSolutions:- Increase
maximum-pool-size - Check for connection leaks (unclosed connections)
- Reduce
connection-timeout - Monitor active connections:
SELECT * FROM pg_stat_activity;
Production Recommendations
Use Managed Databases
Consider Amazon RDS, Google Cloud SQL, or Azure Database for PostgreSQL
Enable SSL/TLS
Configure
ssl=true in JDBC URL and provide certificatesImplement Migrations
Use Flyway or Liquibase instead of
ddl-auto=updateMonitor Performance
Use pg_stat_statements and connection pool metrics
Configure Backups
Automated daily backups with point-in-time recovery
Optimize Queries
Add indexes, use EXPLAIN ANALYZE, monitor slow queries