Overview
The Library Management API uses PostgreSQL as its database and Hibernate/JPA for object-relational mapping. This guide covers all database-related configuration properties and connection setup.Database Connection
Connection Properties
The following properties configure the PostgreSQL database connection:| Property | Type | Description | Example |
|---|---|---|---|
spring.datasource.url | String | PostgreSQL JDBC connection URL | jdbc:postgresql://localhost:5432/library_db |
spring.datasource.username | String | Database username | postgres |
spring.datasource.password | String | Database password | yourpassword |
spring.datasource.driver-class-name | String | JDBC driver class | org.postgresql.Driver |
Environment Variable Configuration
The application is configured to read database credentials from environment variables:db_url- Full PostgreSQL JDBC connection stringdb_username- Database user with appropriate permissionsdb_password- Database user password
Example Database URL Formats
JPA/Hibernate Configuration
Hibernate Properties
| Property | Value | Description |
|---|---|---|
spring.jpa.database-platform | org.hibernate.dialect.PostgreSQLDialect | Specifies PostgreSQL-specific SQL dialect |
spring.jpa.hibernate.ddl-auto | create-drop | Database schema generation strategy |
DDL Auto Modes
Thespring.jpa.hibernate.ddl-auto property controls schema management:
create-drop (Default)
create-drop (Default)
- Behavior: Creates schema at application startup, drops it at shutdown
- Use Case: Development and testing
- Warning: All data is lost when the application stops
- Configured in:
application.properties:13
update
update
- Behavior: Updates schema to match entities without dropping data
- Use Case: Development when you want to preserve data
- Caution: May not handle all schema changes correctly
validate
validate
- Behavior: Validates schema matches entities, no modifications
- Use Case: Production environments
- Recommendation: Use with migration tools like Flyway or Liquibase
none
none
- Behavior: No schema management by Hibernate
- Use Case: Production with external schema management
Production Configuration Recommendations
For production deployments, consider these configuration changes:Connection Pool Settings
JPA Production Settings
SSL/TLS Connection
Troubleshooting
Connection Refused Error
Connection Refused Error
Error:
Connection to localhost:5432 refusedSolutions:- Verify PostgreSQL is running:
systemctl status postgresql - Check the port in your connection URL matches PostgreSQL configuration
- Ensure firewall allows connections on port 5432
Authentication Failed
Authentication Failed
Error:
FATAL: password authentication failed for userSolutions:- Verify environment variables are set correctly
- Check PostgreSQL
pg_hba.confauthentication settings - Ensure the user exists:
psql -U postgres -c "\du"
Database Does Not Exist
Database Does Not Exist
Error:
FATAL: database "library_db" does not existSolutions:- Create the database:
CREATE DATABASE library_db; - Verify the database name in your connection URL
Schema Creation Issues
Schema Creation Issues
Error: Schema validation or creation failuresSolutions:
- Check user has CREATE permissions
- Review Hibernate logs for specific errors
- Verify entity mappings are correct
Related Configuration
- Security Configuration - JWT and authentication setup
- Application Properties - Complete properties reference