IntelliJ运行Spring Boot应用时PostgreSQL DDL执行报错求助
解决Spring Boot连接PostgreSQL时DDL创建表报错schema "C:\Users\Public"不存在的问题
错误原因
报错提示的C:\Users\Public是本地系统路径,并非PostgreSQL中合法的schema名称,问题出在application.properties的数据库配置里,错误地将本地路径指定为了PostgreSQL的schema参数。
修复步骤
1. 修正application.properties中的数据库配置
检查以下关键配置项,确保schema参数为PostgreSQL合法值(默认schema为public):
错误配置示例(常见问题场景):
# 错误:URL中误将本地路径设为schema spring.datasource.url=jdbc:postgresql://localhost:5432/your_db?currentSchema=C:\Users\Public # 或错误设置default_schema为本地路径 spring.jpa.properties.hibernate.default_schema=C:\Users\Public
正确配置示例:
# 数据库连接URL(默认使用public schema) spring.datasource.url=jdbc:postgresql://localhost:5432/your_db_name # 若需显式指定public schema,可添加参数 # spring.datasource.url=jdbc:postgresql://localhost:5432/your_db_name?currentSchema=public # 驱动类 spring.datasource.driver-class-name=org.postgresql.Driver # 数据库用户名 spring.datasource.username=your_username # 数据库密码 spring.datasource.password=your_password # JPA默认schema配置 spring.jpa.properties.hibernate.default_schema=public # 自动建表策略(按需调整:create/update/validate等) spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true
2. 验证PostgreSQL schema存在性
如果需要使用自定义schema而非默认的public,需先在PostgreSQL中手动创建该schema:
CREATE SCHEMA IF NOT EXISTS your_custom_schema;
之后将配置中的currentSchema和default_schema值替换为这个自定义schema名称即可。
3. 检查实体类注解
确认Question实体类的@Table注解没有错误指定schema属性:
// 错误示例 @Table(name = "question", schema = "C:\\Users\\Public") // 正确示例(默认public schema可省略schema属性) @Table(name = "question") // 或指定合法schema @Table(name = "question", schema = "public")
4. 重启应用验证
修改配置后重启Spring Boot应用,查看控制台日志,确认DDL语句正常执行,question表成功生成。
内容的提问来源于stack exchange,提问作者PALVAI THRINADH
相关产品推荐
相关产品推荐

