I'm trying to learn spring boot by following a tutorial, but I can't seem to connect to my postgresql database in docker. Works perfectly fine with a local database.
My docker-compose.yml:
services:
db:
image: postgres
ports:
- "5432:5432"
restart: always
environment:
POSTGRES_PASSWORD: admin
POSTGRES_USER: postgres
My application.properties:
spring.datasource.url=jdbc:postgresql://db:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=admin
spring.datasource.driver-class-name=org.postgresql.Driver
Instead of connecting I keep getting the following error:
org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection
and my docker log doesn't show any attempt to log in.
I'm trying to learn spring boot by following a tutorial, but I can't seem to connect to my postgresql database in docker. Works perfectly fine with a local database.
My docker-compose.yml:
services:
db:
image: postgres
ports:
- "5432:5432"
restart: always
environment:
POSTGRES_PASSWORD: admin
POSTGRES_USER: postgres
My application.properties:
spring.datasource.url=jdbc:postgresql://db:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=admin
spring.datasource.driver-class-name=org.postgresql.Driver
Instead of connecting I keep getting the following error:
org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection
and my docker log doesn't show any attempt to log in.
Share Improve this question asked Feb 5 at 9:42 KallydiKallydi 32 bronze badges1 Answer
Reset to default 0You run your application in local machine or in docker?
- If you run in your local machine, i think correct property config is that:
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
- If you run your application in docker, you have to make sure that both your application and db image are sharing same network. You can config docker network like this: