在Spring Boot启动时创建Postgres模式

3

当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

默认模式是public,我需要更改它并创建自己的模式,在env中定义。
2个回答

2

请在您的 application.properties 文件中使用以下内容:

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

在您的模式文件中添加以下内容:
CREATE TABLE IF NOT EXISTS...

1
你可以在src/main/resources中放置一个schema-postgresql.sql文件,内容如下:

CREATE SCHEMA IF NOT EXISTS ;


你的application.properties文件中似乎已经包含了所有需要的内容。 你可以参考这个链接(https://github.com/AbhijeetSakhalkar/SprintBoot/blob/master/src/main/resources/application.properties),我已经测试过并且正常工作。请确保你的PostgreSQL版本高于9.1。 - abj1305
我收到一个错误,说:关系模式名称不存在。 - nirvair
我猜你的PostgreSQL版本比较老。 - abj1305
现在是9.3点。我将在几分钟内附上相关的堆栈跟踪信息。 - nirvair
一样的。我还添加了SQL文件。 - nirvair
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接