Overview
The Library API uses PostgreSQL as its primary database and Spring Data JPA for object-relational mapping. This guide covers database setup, configuration, and schema management.PostgreSQL Setup
Create the database
Connect to PostgreSQL and create the library database:In the PostgreSQL prompt:
Replace
your_secure_password with a strong password. Keep this password secure as you’ll need it for the application configuration.Application Properties Configuration
Configure the database connection insrc/main/resources/application.properties:
Basic Configuration
application.properties
Configuration Properties Explained
JDBC connection URL for PostgreSQL. Format:
jdbc:postgresql://host:port/database- host: Database server address (default:
localhost) - port: PostgreSQL port (default:
5432) - database: Database name (default:
library)
Database username. Default PostgreSQL user is
postgres.Database password. Leave empty if no password is set (not recommended for production).
JDBC driver class. For PostgreSQL:
org.postgresql.DriverControls database schema generation. Options:
update: Updates schema without dropping existing data (recommended for development)create: Drops and recreates schema on each startup (use with caution)create-drop: Creates schema on startup, drops on shutdownvalidate: Validates schema without making changesnone: Disables automatic schema management
Advanced JPA Configuration
For more control over JPA behavior, add these optional properties:application.properties
Database Schema
The Library API automatically creates the following tables based on JPA entities:Users Table
Books Table
Authors Table
Rentals Table
With
spring.jpa.hibernate.ddl-auto=update, Hibernate automatically creates these tables on application startup if they don’t exist.Entity Relationships
The database schema includes the following relationships:Environment-Specific Configuration
For different environments (development, staging, production), use Spring profiles:- Development
- Production
- Testing
Create Run with:
application-dev.properties:Database Migration
For production environments, consider using a database migration tool instead ofddl-auto=update:
Using Flyway
Add Flyway dependency topom.xml:
application.properties:
src/main/resources/db/migration/:
V1__create_tables.sql
Troubleshooting
Connection refused
Connection refused
Error:
Connection to localhost:5432 refusedSolutions:- Verify PostgreSQL is running:
sudo systemctl status postgresql - Check if PostgreSQL is listening on port 5432:
sudo netstat -plnt | grep 5432 - Verify firewall settings allow connections to port 5432
Authentication failed
Authentication failed
Error:
FATAL: password authentication failed for user "postgres"Solutions:- Verify username and password in
application.properties - Check PostgreSQL authentication configuration in
pg_hba.conf - Try resetting the password:
Database does not exist
Database does not exist
Error:
FATAL: database "library" does not existSolutions:- Create the database:
sudo -u postgres createdb library - Verify database exists:
sudo -u postgres psql -c "\l"
Schema validation failed
Schema validation failed
Error:
Schema-validation: missing table [users]Solutions:- Change
spring.jpa.hibernate.ddl-autotoupdateorcreate - Manually create the required tables
- Run database migrations if using Flyway/Liquibase
Connection pool exhausted
Connection pool exhausted
Error:
Unable to acquire JDBC ConnectionSolutions:- Increase connection pool size:
- Check for connection leaks in your code
- Reduce connection timeout if connections are being held too long
Best Practices
Use Environment Variables
Never commit database credentials to version control. Use environment variables or external configuration.
Connection Pooling
Configure appropriate pool sizes based on your application load and database capacity.
Schema Management
Use
validate or migration tools in production instead of update or create.Monitoring
Enable SQL logging in development, but disable in production for security and performance.
Next Steps
Security Configuration
Learn how to secure your API endpoints
API Reference
Explore the available endpoints