代码之家  ›  专栏  ›  技术社区  ›  Kallydi

jdbc无法连接到docker中的postgresql数据库

  •  0
  • Kallydi  · 技术社区  · 1 年前

    我试图通过教程学习spring boot,但我似乎无法连接到docker中的postgresql数据库。与本地数据库配合使用非常好。

    我的docker-compose.yml:

    services:
      db:
        image: postgres
        ports:
          - "5432:5432"
        restart: always
        environment:
          POSTGRES_PASSWORD: admin
          POSTGRES_USER: postgres
    

    我的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
    

    我没有连接,而是不断收到以下错误: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection

    我的docker日志没有显示任何登录尝试。

    1 回复  |  直到 1 年前
        1
  •  0
  •   Kezlo    1 年前

    您是在本地计算机上运行应用程序还是在docker中运行应用程序?

    • 如果你在本地机器上运行,我认为正确的属性配置是: spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
    • 如果你在docker中运行应用程序,你必须确保你的应用程序和db-image共享同一个网络。你可以这样配置docker网络:

    Postgres Docker Compose

    推荐文章