postgresqlspring-bootdockerjdbcdocker-compose

jdbc fails to connect to a postgresql database in docker


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.


Solution

  • You run your application in local machine or in docker?

    Postgres Docker Compose