Skip to main content

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:
PropertyTypeDescriptionExample
spring.datasource.urlStringPostgreSQL JDBC connection URLjdbc:postgresql://localhost:5432/library_db
spring.datasource.usernameStringDatabase usernamepostgres
spring.datasource.passwordStringDatabase passwordyourpassword
spring.datasource.driver-class-nameStringJDBC driver classorg.postgresql.Driver
Database credentials should never be hardcoded in application.properties. Use environment variables or external configuration management.

Environment Variable Configuration

The application is configured to read database credentials from environment variables:
spring.datasource.url=${db_url}
spring.datasource.username=${db_username}
spring.datasource.password=${db_password}
Required Environment Variables:
  • db_url - Full PostgreSQL JDBC connection string
  • db_username - Database user with appropriate permissions
  • db_password - Database user password

Example Database URL Formats

export db_url="jdbc:postgresql://localhost:5432/library_db"
export db_username="postgres"
export db_password="admin123"

JPA/Hibernate Configuration

Hibernate Properties

PropertyValueDescription
spring.jpa.database-platformorg.hibernate.dialect.PostgreSQLDialectSpecifies PostgreSQL-specific SQL dialect
spring.jpa.hibernate.ddl-autocreate-dropDatabase schema generation strategy

DDL Auto Modes

The spring.jpa.hibernate.ddl-auto property controls schema management:
  • 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
  • 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
spring.jpa.hibernate.ddl-auto=update
  • Behavior: Validates schema matches entities, no modifications
  • Use Case: Production environments
  • Recommendation: Use with migration tools like Flyway or Liquibase
spring.jpa.hibernate.ddl-auto=validate
  • Behavior: No schema management by Hibernate
  • Use Case: Production with external schema management
spring.jpa.hibernate.ddl-auto=none
The default create-drop setting is only suitable for development. For production environments, use validate or none with proper database migration tools.

Production Configuration Recommendations

For production deployments, consider these configuration changes:

Connection Pool Settings

# HikariCP connection pool configuration
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.idle-timeout=600000
spring.datasource.hikari.max-lifetime=1800000

JPA Production Settings

# Disable schema auto-generation
spring.jpa.hibernate.ddl-auto=validate

# Enable SQL logging in development only
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.format_sql=false

# Performance optimizations
spring.jpa.properties.hibernate.jdbc.batch_size=20
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true

SSL/TLS Connection

spring.datasource.url=jdbc:postgresql://your-host:5432/library_db?sslmode=require&ssl=true

Troubleshooting

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
Error: FATAL: password authentication failed for userSolutions:
  • Verify environment variables are set correctly
  • Check PostgreSQL pg_hba.conf authentication settings
  • Ensure the user exists: psql -U postgres -c "\du"
Error: FATAL: database "library_db" does not existSolutions:
  • Create the database: CREATE DATABASE library_db;
  • Verify the database name in your connection URL
Error: Schema validation or creation failuresSolutions:
  • Check user has CREATE permissions
  • Review Hibernate logs for specific errors
  • Verify entity mappings are correct

Build docs developers (and LLMs) love