PostgreSQL configuration
The application connects to PostgreSQL using the following configuration fromapplication.properties:
JPA and Hibernate settings
OrgStack uses conservative JPA settings optimized for production:Key configuration details
hibernate.ddl-auto=validate
hibernate.ddl-auto=validate
This setting prevents Hibernate from automatically modifying the database schema. When set to
validate, Hibernate will:- Verify that the database schema matches your entity mappings
- Throw an exception if there are mismatches
- Never create, update, or drop tables automatically
show-sql=false
show-sql=false
SQL logging is disabled to reduce log verbosity in production. Enable this in development environments to debug queries:
open-in-view=false
open-in-view=false
The Open Session in View pattern is disabled, which means:
- Database sessions are not kept open for the entire HTTP request
- Lazy loading only works within the service layer transaction
- Better performance and resource management
- Prevents common N+1 query problems from being hidden
With this setting, you must eagerly fetch all required data within your service layer transactions. Attempting to access lazy-loaded relationships in controllers or views will throw
LazyInitializationException.Entity auditing
OrgStack enables JPA auditing throughJpaConfig.java to automatically track creation and modification timestamps:
BaseEntity, which provides:
id(UUID): Auto-generated unique identifiercreatedAt(Instant): Automatically set when entity is createdupdatedAt(Instant): Automatically updated on every modification
Migration strategy
Sincehibernate.ddl-auto is set to validate, you must manage schema changes explicitly. OrgStack recommends using database migration tools for version-controlled schema management.
Recommended approach: Flyway or Liquibase
Manual migration process
If you prefer manual migrations:- Generate DDL from entities: Use Hibernate’s schema export tool in development
- Review the generated SQL: Ensure it matches your intentions
- Create migration script: Write idempotent SQL that can safely run multiple times
- Test in staging: Verify the migration works with production-like data
- Apply to production: Run during maintenance window with proper backups
When creating new entities or modifying existing ones, run your application with
hibernate.ddl-auto=update in a development environment to see what schema changes Hibernate would make. Then translate those changes into a migration script.Backup and restore
Regular backups are critical for data protection and disaster recovery.Backup strategies
Logical backups with pg_dump
Logical backups with pg_dump
Use Advantages:
pg_dump for logical backups that export data as SQL statements:- Portable across PostgreSQL versions
- Can restore individual tables or schemas
- Human-readable SQL format available
- Slower for large databases
- Requires more storage than physical backups
Physical backups with pg_basebackup
Physical backups with pg_basebackup
Use Advantages:
pg_basebackup for physical backups that copy the entire data directory:- Faster for large databases
- Smaller backup size
- Can be used for point-in-time recovery with WAL archiving
- Must restore entire cluster
- Less portable across PostgreSQL versions
Continuous archiving with WAL
Continuous archiving with WAL
For point-in-time recovery, enable Write-Ahead Log (WAL) archiving:This allows you to restore to any point in time between backups.
Automated backup schedule
Set up automated daily backups using cron:/usr/local/bin/backup_orgstack.sh):
Restore procedures
Database credentials management
Environment-based configuration
Use environment variables for sensitive configuration:Using Spring Cloud Config
For centralized configuration management:Set up Config Server
Deploy a Spring Cloud Config Server to manage configuration across environments.
Using Docker Secrets
If deploying with Docker Swarm:Using Kubernetes Secrets
For Kubernetes deployments:Performance optimization
Connection pool tuning
Monitor connection pool usage and adjust based on your workload:A good starting point for
maximum-pool-size is: (available_connections / number_of_app_instances) - 10% bufferPostgreSQL optimization
Key PostgreSQL settings for production:Index management
Regularly monitor and maintain indexes:OrgStack uses UUID primary keys generated in the application. Consider adding indexes on frequently queried columns like timestamps, foreign keys, and commonly filtered fields.