Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create a schema in postgres while spring boot starts

I need to create a new schema in Postgres when the spring boot loads. So, it should check if the schema doesn't exist then create a new schema. I am using application.properties for database configuration.

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

The default schema that Postgres uses is public, I need to change that make my own schema, which I'll define in env.

like image 932
nirvair Avatar asked Aug 08 '26 01:08

nirvair


2 Answers

Use the following your application.properties file:

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

In your schema file add the following:

CREATE TABLE IF NOT EXISTS...
like image 150
mukut bhattacharjee Avatar answered Aug 09 '26 16:08

mukut bhattacharjee


You can have a schema-postgresql.sql file placed in src/main/resources with content: as follows:

CREATE SCHEMA IF NOT EXISTS ;

like image 42
abj1305 Avatar answered Aug 09 '26 17:08

abj1305



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!