How do I specify database schema used by Spring Boot? I am using default hibernate (=default) and postgres (but i hoping for a generic solution). I know how to specify JDBC URL:
spring.datasource.url=jdbc:postgresql:db_name
But unfortunately postgresql does not allow to specify schema in JDBC URL. I know that there is hibernate property hibernate.default_schema
, so I was hoping that one of the following properties will work:
hibernate.default_schema=schema spring.hibernate.default_schema=schema spring.jpa.hibernate.default_schema=raw_page
But unfortunately neither of them seems to have any result.
there is added support for specifying the current schema using URL. With @ClassRule, we create an instance of PostgreSQL database container. Next, in the setup method, create a connection to that database and create the required objects. To change the default schema, we need to specify the currentSchema parameter.
Until now with spring 4 and XML configuration I was able to only put the DB URL like: jdbc:mysql://180.179.57.114:3306/?zeroDateTimeBehavior=convertToNull and in the entity class specify the schema to use and thus able to connect to multiple schemas.
Spring Boot can automatically create the schema (DDL scripts) of your DataSource and initialize it (DML scripts). It loads SQL from the standard root classpath locations: schema. sql and data. sql , respectively. In addition, Spring Boot processes the schema-${platform}.
Use for application.properties
:
spring.jpa.properties.hibernate.default_schema=your_scheme
OR for application.yaml
:
spring: jpa: properties: hibernate.default_schema: your_scheme
From the Spring Boot reference guide:
all properties in
spring.jpa.properties.*
are passed through as normal JPA properties (with the prefix stripped) when the localEntityManagerFactory
is created
See http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-jpa-properties
For a full list of available properties see http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-jpa-properties
It depends on the DataSource implementation which property has to be used to set the default schema (reference). With HikariDataSource for example spring.jpa.properties.hibernate.default_schema
is ignored and you have to set
spring.datasource.hikari.schema=schema
See the complete list of HikariCP configuration parameters here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With