最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

spring boot - jdbc fails to connect to a postgresql database in docker - Stack Overflow

programmeradmin11浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

You 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:

发布评论

评论列表(0)

  1. 暂无评论