3

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.

2 Answers 2

2

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...
Sign up to request clarification or add additional context in comments.

Comments

1

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

CREATE SCHEMA IF NOT EXISTS ;

10 Comments

Do I need to add anything in application.properties?
You have all the things covered in your application.properties it seems. You can refer this link as I have tested it and is working. Make sure your postgres version is later to 9.1
I get an error saying: Relation schema_name doesn't exist.
Can you paste your stacktrace ??
I guess your postgres version is older
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.