I’m currently developing a Java-based Library Management System (LMS) application. The tech stack includes Spring Boot for the backend, JavaFX for the frontend, JUnit for testing, and PostgreSQL as the database. The database is containerized using Docker with the following docker-compose.yml
configuration:
version: '3'
services:
postgres:
image: postgres:14.2
container_name: postgres
ports:
- "5432:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
volumes:
- ./src/main/resources/initial_data_generation.sql:/docker-entrypoint-initdb.d/init.sql
In it, the path to my database initialization file has already granted ownership of the database to the postgres
user using a command (see the file for more details):
...
CREATE DATABASE "library-system-db" WITH TEMPLATE = template0 ENCODING = 'UTF8' LOCALE = 'en_US.utf8';
ALTER DATABASE "library-system-db" OWNER TO postgres;
\connect -reuse-previous=on "dbname='library-system-db'"
...
Additionally, the application.properties
file is configured to connect to the database after the container is up:
## Database connection
spring.datasource.url=jdbc:postgresql://${DATABASE_HOST:localhost}:${DATABASE_PORT:5432}/${DATABASE_NAME:library-system-db}
spring.datasource.username=${DATABASE_USERNAME:postgres}
spring.datasource.password=${DATABASE_PASSWORD:password}
# JPA Properties
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL10Dialect
After running docker compose up -d
, I checked the logs and confirmed the container is running properly. I also accessed the container terminal to verify directly and saw that the database was created.
However, when I run the application via the main()
method, the build fails and throws the error:
FATAL: database "library-system-db" does not exist
You can find more details in the attached log file. The log also suggests that a few other errors might be stemming from this root issue.
I’d really appreciate any suggestions or fixes you might have to resolve this problem 🙏
P.S. The project I’m developing is based on the Github source code on by Kwame Mintah. Let me know if you need any additional info!
To begin troubleshooting, I kindly recommend verifying whether there is any local instance of PostgreSQL running on your machine, aside from the Dockerized version. If such an instance is running, please stop or terminate it to avoid potential conflicts.
Next, I suggest modifying your application properties to use the actual host IP address instead of localhost
. Specifically, please replace localhost
with 127.0.0.1
in the following configuration:
spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/library-system-db
This adjustment should resolve any connection issues you're encountering.
In my experience, it has occasionally occurred that the hosts file was not updated, which can prevent the Docker instance from being accessible through localhost
. Using the direct host IP address, 127.0.0.1
, should mitigate this issue.
Also timebeing you are trouble shooting, no need to load values from properties, let's direct use hostname, port and databaseName
Please update the configuration and monitor the behavior.