【问题标题】:create a schema in postgres while spring boot starts在 Spring Boot 启动时在 postgres 中创建一个模式
【发布时间】:2019-04-06 12:25:50
【问题描述】:

当 Spring Boot 加载时,我需要在 Postgres 中创建一个新模式。因此,它应该检查架构是否不存在,然后创建一个新架构。 我正在使用 application.properties 进行数据库配置。

spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://${vcap.services.postgresql-lite.credentials.hostname}:${vcap.services.postgresql-lite.credentials.port}/${vcap.services.postgresql-lite.credentials.dbname}
spring.datasource.username=${vcap.services.postgresql-lite.credentials.username}
spring.datasource.password=${vcap.services.postgresql-lite.credentials.password}
spring.jpa.properties.hibernate.default_schema=${DATABASE_SCHEMA}
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

Postgres 使用的默认架构是公共的,我需要更改它以创建我自己的架构,我将在 env.xml 中定义它。

【问题讨论】:

    标签: spring postgresql spring-mvc spring-boot


    【解决方案1】:

    使用以下您的 application.properties 文件:

    spring.datasource.initialize=true
    spring.datasource.schema=${DB_SCHEMA}
    

    在您的架构文件中添加以下内容:

    CREATE TABLE IF NOT EXISTS...
    

    【讨论】:

      【解决方案2】:

      您可以将 schema-postgresql.sql 文件放在 src/main/resources 中,内容如下:

      如果不存在则创建模式;

      【讨论】:

      • 我需要在 application.properties 中添加什么吗?
      • 您的 application.properties 中似乎涵盖了所有内容。您可以参考此link,因为我已经对其进行了测试并且正在工作。确保您的 postgres 版本高于 9.1
      • 我收到一条错误消息:关系模式名称不存在。
      • 你能粘贴你的堆栈跟踪吗??
      • 我猜你的 postgres 版本比较旧
      猜你喜欢
      • 2018-12-19
      • 2020-07-23
      • 2020-10-02
      • 2019-11-22
      • 2020-07-14
      • 1970-01-01
      • 1970-01-01
      • 2022-07-12
      • 1970-01-01
      相关资源
      最近更新 更多