I am unable to connect my spring boot application to a PostgreSQL database using Spring JPA.
Below is my application.yaml file.
spring:
application:
name: jaah
datasource:
url: jdbc:postgresql://db:5432/taah
username: waah
password: blah
driverClassName: org.postgresql.Driver
jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
ddl-auto: none
boot:
'[allow_jdbc_metadata_access]': false
sql:
init:
mode: never
main:
allow-bean-definition-overriding: true
logging:
level:
org:
hibernate: DEBUG
server:
port: 8080
address: 0.0.0.0
and below is the docker compose.yaml file. The database is hosted on post 5432 INSIDE the container and 5434 on the outside.
db:
image: postgres
container_name: db
ports:
- 5434:5432
restart: always
env_file:
- .env
volumes:
- postgresdata:/var/lib/postgresql/data
environment:
- POSTGRES_DB=taah
- POSTGRES_USER=waah
- PGUSER=waah
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD} #from env
healthcheck:
test: ["CMD-SHELL","psql -h localhost -U $${POSTGRES_USER} -c select 1 -d $${POSTGRES_DB}"]
interval: 10s
timeout: 5s
retries: 5
networks:
- mynetwork
spring_boot_server:
image: backend
build: .
depends_on:
db:
condition: service_healthy
ports:
- "8080:8080"
networks:
- mynetwork
environment:
- SERVER_PORT=8080
networks:
mynetwork:
driver: bridge
My DockerFile below
FROM openjdk:21
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
The application worked when I ran the docker compose and the server separately i.e when I ran the postgres docker container and THEN the spring application connected to the database at port 5434.
This was the last message before the server started
db | 2024-09-15 11:14:15.980 UTC [1] LOG: database system is ready to accept connections
I bash'd into the postgres container sudo docker exec -it db /bin/sh
psql -h db -p 5432 -U waah -d taah Password for user waah:
waah=# dt Did not find any relations.
This was AFTER I restarted the container using
- sudo docker-compose down –volumes
- sudo docker-compose build
- sudo docker-compose up
I also changed the database url to
jdbc:postgresql://localhost:5432/taah
but with no avail
2
Answers
I had to
then
and then
Thanks to @life888888 for providing the answer
My Environment:
docker-compose.yaml
version
:version: '3.3'
services:
#- PGUSER=waah
port 5432:5432
condition: service_healthy
Project Tree
Dockerfile
pom.xml
.dockerignore
.env
application.yaml
DemoJpaApplication.java
HelloController.java
WebConfig.java
model/Person.java
repository/PersonRepository.java
service/PersonService.java
controller/PersonController.java
Spring Boot Application Build
Docker-compose Run
Delete Old Docker Image
find
backend
‘s old image iddelete
backend
old imageRun
Check container status
run command:
return:
Test Backend
Test 1. Create a Person (POST)
get return
Test 2. Read All Persons with Pagination (GET)
get return
swagger ui test
http://localhost:8080/swagger-ui/index.html