【问题标题】:Only in Linux; org.postgresql.util.PSQLException: ERROR: relation "table_name" does not exist仅在 Linux 中; org.postgresql.util.PSQLException:错误:关系“table_name”不存在
【发布时间】:2020-10-02 19:58:28
【问题描述】:

我一直在查看 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 时,它可以工作。

【问题讨论】:

  • 你可以试试spring.jpa.properties.hibernate.default_schema=public 吗?
  • 嗨,这是我帖子中 application.properties 的最后一行 ;)
  • 不,不一样,顺便说一句,将您的spring.jpa.hibernate.ddl-auto=none 更改为createupdate(如果有数据,请备份您的数据库)
  • 我确实按照您的建议将spring.jpa.hibernate.ddl-auto 设置为update。它在创建表格的意义上起作用。但问题是,我不知道在哪里!我通过应用程序填充了一些数据,但是当我使用 DBeaver 作为用户名连接到数据库时,我看到了公共架构,但什么也没有。就像有一个隐藏的模式,其中包含表格和 Hibernate 创建的数据。我迷路了!
  • 最后是数据库名称的问题。我不明白它如何在本地而不是在部署时工作

标签: postgresql amazon-web-services spring-boot hibernate


【解决方案1】:

对于诊断问题,您应该在

处使用纯文本
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

【讨论】:

  • 感谢您的回答。它是 Spring Boot 2.3.3.RELEASE 和 Postgres 12.3
  • 休眠映射表示表table_name 不存在。这意味着连接信息是正确的。问题可能与不正确的架构有关
  • 这是我首先想到的,但是当我将本地项目(Windows)连接到远程数据库时,它运行良好。仅当我在 AWS 环境 (Linux) 中部署项目时才会发生这种情况。
  • 感谢您的帮助,它帮助我发现它不是正确的数据库名称。
【解决方案2】:

最后我使用了错误的数据库名称。我使用postgres 作为数据库名称。相反,它是ebdb。我不明白如何允许我通过 DBeaver 或 PgAdmin 连接到此 postgres 或在本地运行应用程序时我如何能够连接到此数据库名称,而不是在应用程序部署在 AWS 中时。

文档中提到了该名称:https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-rds.html

您还可以在数据库实例的 AWS RDS 配置选项卡中查看名称。

【讨论】:

    猜你喜欢
    • 2020-09-03
    • 2017-04-11
    • 2015-10-09
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 2012-01-13
    相关资源
    最近更新 更多