Database overview
The application uses PostgreSQL for storing all persistent data including:- User accounts and authentication data
- Book catalog and metadata
- Borrowing records and transaction history
- JWT refresh tokens
The application uses Spring Data JPA with Hibernate for database interactions, providing an abstraction layer over direct SQL.
Docker deployment
When using Docker Compose, PostgreSQL is automatically configured and deployed.Container configuration
The database service runs PostgreSQL 15 on Alpine Linux:compose.yaml
Key configuration details
- Image:
postgres:15-alpine- Lightweight PostgreSQL 15 image - Port:
5432(standard PostgreSQL port) - Volume: Named volume
postgres_datafor data persistence - Restart policy:
always- Automatically restarts on failure - Health check: Uses
pg_isreadyto verify database availability
Database credentials
Configure your database credentials in the.env file:
.env
Password security best practices
Store securely
Keep your
.env file secure and never commit it to version control. The repository’s .gitignore already excludes it.Hibernate and JPA configuration
The application uses Spring Data JPA with Hibernate for database operations.Schema management
application.properties
update, which:
- Automatically creates tables on first run
- Updates schema when entity models change
- Preserves existing data during updates
Available DDL modes
| Mode | Behavior | Use Case |
|---|---|---|
create | Drops and recreates schema on startup | Testing (data loss) |
create-drop | Creates schema on startup, drops on shutdown | Testing (data loss) |
update | Updates schema without data loss | Development |
validate | Validates schema matches entities | Production |
none | No schema management | Production with migrations |
SQL logging
application.properties
Disable SQL logging in production by setting
spring.jpa.show-sql=false to improve performance and reduce log volume.PostgreSQL dialect
application.properties
Database connection
The Spring Boot application connects to PostgreSQL using environment variables.Connection string
application.properties
compose.yaml
Connection URL format
Docker Compose (container-to-container):The hostname
database refers to the Docker Compose service name, enabling container-to-container communication.Data persistence
PostgreSQL data is stored in a named Docker volume to ensure persistence across container restarts.Volume configuration
compose.yaml
library-management-system_postgres_data (prefixed with the project name).
Managing the data volume
Database backups
Regular backups are essential for production environments.Create a backup
Restore from backup
Health checks
The database container includes a health check to ensure availability before dependent services start.compose.yaml
Health check parameters
- Test command:
pg_isreadychecks if PostgreSQL is accepting connections - Interval: Runs every 10 seconds
- Timeout: Fails if the check takes more than 5 seconds
- Retries: Allows 5 failures before marking as unhealthy
Check database health
Connecting to the database
You can connect to the PostgreSQL database directly for administration or debugging.Using psql in the container
Using a database client
Connect using any PostgreSQL client with these credentials:- Host:
localhost - Port:
5432 - Database:
library_db(or your configured name) - Username:
library_admin(or your configured username) - Password: Your configured password
Common psql commands
Once connected to the database:Performance optimization
Connection pooling
Spring Boot automatically configures HikariCP connection pooling with sensible defaults. You can customize the pool size inapplication.properties:
Database indexing
Ensure proper indexes are defined in your JPA entities using annotations:Troubleshooting
Database container won’t start
Verify credentials
Ensure your
.env file has all required variables:POSTGRES_USERPOSTGRES_PASSWORDPOSTGRES_DB
Backend cannot connect to database
Check connection string
Ensure the JDBC URL uses the correct hostname:
- Docker:
jdbc:postgresql://database:5432/library_db - Local:
jdbc:postgresql://localhost:5432/library_db
Schema update errors
If Hibernate fails to update the schema:- Check entity definitions - Ensure JPA entities are properly annotated
- Review SQL logs - Enable
spring.jpa.show-sql=trueto see what Hibernate is attempting - Manual migration - For complex changes, consider creating manual migration scripts
- Restart fresh - As a last resort, drop and recreate the database:
Production considerations
Use managed database services
For production deployments, consider using managed PostgreSQL services:- AWS RDS for PostgreSQL
- Google Cloud SQL for PostgreSQL
- Azure Database for PostgreSQL
- DigitalOcean Managed Databases
- Automated backups
- High availability
- Automated updates
- Monitoring and alerting
- Scalability
Security hardening
Monitoring
Monitor key database metrics:- Connection pool utilization
- Query performance and slow queries
- Database size and growth rate
- Cache hit ratios
- Replication lag (if using replicas)
- pg_stat_statements for query analysis
- pgBadger for log analysis
- Prometheus with postgres_exporter
- Datadog, New Relic, or similar APM tools