【问题标题】:Spring Boot - How to set the default schema for PostgreSQL?Spring Boot - 如何为 PostgreSQL 设置默认模式?
【发布时间】:2015-08-31 03:59:23
【问题描述】:

我目前在 application.properties 文件中所做的是:

spring.datasource.url=jdbc:postgresql://localhost:5432/myDB?currentSchema=mySchema

这没有其他属性吗?由于它看起来很hacky并且根据帖子(仍然找到链接抱歉,稍后会更新),它仅适用于PostgreSQL 9.4。

【问题讨论】:

    标签: java spring hibernate postgresql


    【解决方案1】:

    由于这很老而且还没有正确的答案,所以要设置的正确属性如下:

    spring.jpa.properties.hibernate.default_schema=yourschema
    

    【讨论】:

    • 那么 JDBC 配置,而不是 JPA 呢?
    • 为 JDBCTemplate 设置 currentSchema=mySchema 不起作用。我正在使用 postgres 12。
    【解决方案2】:

    您可以尝试为 jdbc 用户设置默认架构。

    1) ALTER USER user_name SET search_path to 'schema'

    2) 你试过这个属性吗? spring.datasource.schema

    http://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html

    【讨论】:

    • 谢谢!我尝试了这个并且它有效,但是我尽可能地想在我的应用程序中的某个地方设置它(我也不想在某个地方为此执行 sql 文件)。
    【解决方案3】:

    只有 SpringBoot 2.5.5 对我有用:

    应用程序属性

    spring.datasource.hikari.schema=my_schema
    

    aplication.yml 版本:

    spring:
      datasource:
        hikari:
          schema: my_schema
    

    【讨论】:

      猜你喜欢
      • 2013-10-26
      • 2019-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-22
      • 2016-08-01
      • 1970-01-01
      相关资源
      最近更新 更多