首页 > 解决方案 > 仅在 Linux 中;org.postgresql.util.PSQLException:错误:关系“table_name”不存在

问题描述

我一直在寻找 Stack Overflow 和论坛,但到目前为止还没有运气。我有一个 Spring Boot 2.3.3.RELEASE、JPA/Hibernate 堆栈。

我有一张桌子

CREATE TABLE table_name (...)

我创建了没有引号和小写的表格。它在本地运行良好。在本地,我在带有 Amazon Corretto jdk11.0.8_10 的 Windows 环境中。

现在我已经创建了一个 AWS RDS PostreSQL 数据库实例。它托管在 Linux 上。当我从 Windows 连接到数据库时,它工作正常。但是当我在 AWS Elastic Beanstalk 上部署我的应用程序时,当 Hibernate 尝试查询数据库时,我遇到了错误

org.postgresql.util.PSQLException: ERROR: relation "table_name" does not exist.

我的实体看起来像

@Entity
@Table(name = "table_name")
public class User {
    
}

该表位于默认public架构中。

我的application.properties

spring.datasource.url=jdbc:postgresql://${RDS_HOSTNAME}:${RDS_PORT}/${RDS_DB_NAME}
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.username=${RDS_USERNAME}
spring.datasource.password=${RDS_PASSWORD}
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL10Dialect
spring.jpa.properties.hibernate.globally_quoted_identifiers=false
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.default_schema=public

我是否将架构放在@Table. 我能发现的唯一区别是环境。Windows 与 Linux。当我在本地启动应用程序并使用 AWS RDS DB 时,它可以工作。

标签: postgresqlamazon-web-servicesspring-boothibernate

解决方案


对于诊断问题,您应该在

spring.datasource.url=jdbc:postgresql://${RDS_HOSTNAME}:${RDS_PORT}/${RDS_DB_NAME}
# ...
spring.datasource.username=${RDS_USERNAME}
spring.datasource.password=${RDS_PASSWORD}

你的 Spring Boot,PostgreSQL 是什么版本?

这是样本

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

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto=update

注意:不要使用org.hibernate.dialect.PostgreSQL10Dialect,使用

spring.jpa.database=org.hibernate.dialect.PostgreSQLDialect

推荐阅读