You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker部署PostgreSQL无法通过Java应用登录求助

问题:Spring Boot连接Docker部署的PostgreSQL密码认证失败

环境配置

Docker Compose配置

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

Spring Boot application.properties配置

spring.application.name=demo
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password= test

spring.jpa.hibernate.ddl-auto=update

错误日志

2025-02-22T00:06:39.005+01:00  WARN 30656 --- [demo] [           main] o.h.e.j.e.i.JdbcEnvironmentInitiator     : HHH000342: Could not obtain connection to query metadata

org.hibernate.exception.GenericJDBCException: unable to obtain isolated JDBC connection [KATASTROFALNY: autoryzacja hasłem nie powiodła się dla użytkownika "postgres"] [n/a]
...(省略后续堆栈信息)

解决方案

  • 修正配置文件空格问题:spring.datasource.password= test中密码值前多了一个空格,导致实际传递的密码是 test(带空格),与Docker中设置的test不匹配。修改为:

    spring.datasource.password=test
    
  • 重置PostgreSQL容器(若之前已启动过):PostgreSQL第一次启动时会根据环境变量初始化用户密码,后续修改环境变量不会自动更新。需删除容器和数据卷后重新创建:

    docker-compose down -v
    docker-compose up -d
    
  • 验证连接:使用psql命令测试数据库连接是否正常:

    psql -h localhost -U postgres -d postgres
    

    输入密码test,若能成功进入数据库则配置正确。

内容的提问来源于stack exchange,提问作者IRQ 3

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 05:44:53