Spring Boot - How to set the default schema for PostgreSQL? - java

What I am currently doing in my application.properties file is:
spring.datasource.url=jdbc:postgresql://localhost:5432/myDB?currentSchema=mySchema
Isn't there another property for this? Since it looks hacky and according to a post (still finding the link sorry, will update later), it is only applicable to PostgreSQL 9.4.

Since this is quite old and has no proper answer yet, the right property to set is the following:
spring.jpa.properties.hibernate.default_schema=yourschema

You can try setting the default schema for the jdbc user.
1)
ALTER USER user_name SET search_path to 'schema'
2) Did you try this property?
spring.datasource.schema
http://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html

With SpringBoot 2.5.5 the only thing that work for me:
aplication.properties
spring.datasource.hikari.schema=my_schema
aplication.yml version:
spring:
datasource:
hikari:
schema: my_schema

Related

spring.datasource.data picked up, but script never executed

I am trying to set up in-memory H2 tables for a test class in my Spring boot application.
My config looks something like:
spring:
jpa:
show-sql: true
generate-ddl: true
properties:
hibernate:
dialect: org.hibernate.dialect.H2Dialect
hibernate:
ddl-auto: create-drop
datasource:
# not sure which one to use so added both just in case
initialization-mode: always
initialize: true
platform: h2
# casting a wide net here, but no cookie - completely ignored
data: data-h2.sql,classpath*:data-h2.sql, classpath:data-h2.sql
url: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
driver-class-name: org.h2.Driver
username: sa
password:
As you can see, I'm trying to load a data-h2.sql script upon db initialization.
Unfortunately, the property is ignored no matter the value.
I am certain the configuration file is being picked up properly (e.g. among others, I desperately added a #Value("${spring.datasource.data}" -annotated property in my test class and the value was indeed populated correctly).
As an alternative, I could annotate the test class with #Sql("classpath:data-h2.sql") which did run the script - however it did so for every test, while I wanted the script to be run once before any test execution.
I also tried removing that and using a blank schema.sql and moving the population to data.sql (as suggested here), but Spring would complain about the empty schema file - which is useless to me, because my schema is auto-generated and I certainly don't want to re-create it (NB: probably a conflict with a hibernate property if memory serves).
I've browsed some of the answers here, but the only one I could use, is not working.
The only solution I can see is to keep the #Sql annotation, but try and clear the tables after every test with another #Sql annotation launching another script on #After.
This seems insane to me - there must be a better solution.
Am I missing something more esoteric than it already is in my configuration?
Simply put your file in the /main/resources directory (what you already did)
Spring Boot 2:
The correct property is:
spring.datasource.initialization-mode=always
Read more about this topic in the Spring Boot Documentation: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-initialize-a-database-using-spring-jdbc
Spring Boot 1
You only have to place data-h2.sql in the classpath
Read more about this topic in the Spring Boot Documentation:
https://docs.spring.io/spring-boot/docs/1.5.8.RELEASE/reference/htmlsingle/#howto-initialize-a-database-using-spring-jdbc

spring.datasource.initialize is deprecated

I have a springboot application where I am trying to add following to application.properties file
spring.datasource.initialize=false
When I add this I see a warning as below:
I tried finding out what's the new property that replaces this deprecated property but in vain.
Can anybody help on this!
Having a reference to a migration guide would be great.
In Spring Boot 2.5, 'spring.datasource.initialization-mode' has been depracated as well:
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.5-Release-Notes#SQL-Script-DataSource-Initialization
you should use:
spring.sql.init.mode=always
or
spring.sql.init.mode=never
You can read more at:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.data-initialization
As per the document
Spring Boot automatically creates the schema of an embedded
DataSource. This behaviour can be customized by using the
spring.datasource.initialization-mode property. For instance, if you
want to always initialize the DataSource regardless of its type:
spring.datasource.initialization-mode=always
Look at this migration guide
As per Spring Boot Migration mentioned in Github
Basic DataSource initialization is now only enabled for embedded data
sources and will switch off as soon as you’re using a production
database. The new spring.datasource.initialization-mode (replacing
spring.datasource.initialize) offers more control.
spring.datasource.initialization-mode=always
The property spring.datasource.initialization-mode from Spring boot verion 2.7 and onwards is not any more depracated. It has been completely removed!
So the change into the replacement property spring.sql.init.mode is a must do from now on.
Spring Boot 2.7 changelog
You can use spring.jpa.defer-datasource-initialization. Refer to this Spring Documentation on how to Initialize a Database Using Basic SQL Scripts:
spring.jpa.defer-datasource-initialization=true
spring.sql.init.enabled=true - to initialize database by data.sql script located in application resources
spring.sql.init.enabled=false - to

How to set "max_allowed_packet" property from spring application.yml

I need to change "max_allowed_packet" property for mySQL data base from SPRING property file (application.yml). I found some topics about that, like this.
They proposed to use mySQL command line:
$>mysql --max_allowed_packet={some_value}
But maybe someone know new solution for this task? It would be great to have this ability.
You cannot change --max_allowed_packet property from spring application.yml file, because this is a mysql server property. Therefore you should set this property when you start the mysql server.
You could add it in my.cnf, check this answer

HikariCP Postgresql Driver Claims to not accept JDBC URL

I've pushed my application to cloudfoundry. However every time I connect to my postgresql/elephant sql I received this error
Driver org.postgresql.Driver claims to not accept JDBC URL jdbc:postgres://cwkqmdql:SsVqwdLxQObgaJAYu68O-8gTY1VmS9LX#pellefant.db.elephantsql.com:5432/cwkqmdql/
Is there anything I've missed?
There are a few issues with that URL and a latest PSQL driver may complain.
jdbc:postgres: should be replaced with jdbc:postgresql:
Do not use jdbc:postgresql://<username>:<passwor>..., user parameters instead: jdbc:postgresql://<host>:<port>/<dbname>?user=<username>&password=<password>
In some cases you have to force SSL connection by adding sslmode=require parameter
So your URL should be:
jdbc:postgresql://#pellefant.db.elephantsql.com:5432/cwkqmdql?user=cwkqmdql&password=SsVqwdLxQObgaJAYu68O-8gTY1VmS9LX
or
jdbc:postgresql://#pellefant.db.elephantsql.com:5432/cwkqmdql?user=cwkqmdql&password=SsVqwdLxQObgaJAYu68O-8gTY1VmS9LX&sslmode=require
I hope that will help.
In my case it was defining the property in double quotes in the java.properties file
by changing
jdbcUrl="url"
to
jdbcUrl=url
it works again
The issue I had was that I had given double quotes for the datasource url in the properties file.
What I had given :
spring.datasource.url="jdbc:postgresql://localhost:5432/postgres"
The correct way to give the url is :
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
also had this error, but realized it was because i had typed
postgressql
instead of
postgresql in my url
I had the same issue with h2 DB in WebFlux. The issue was with spring.datasource.driver
Not working
spring.datasource.driver=org.h2.Driver
Working
spring.datasource.driver-class-name=org.h2.Driver
I was having the same error until I realize that I did not set my profile to active in the application.properties class.
spring.profiles.active = dev
Replace
postgresql://localhost/db_name
with
jdbc:postgresql://localhost/db_name
In my case, two configuration files are in different format:
application.properties in src/main/resources
application.yml in src/test/resources
After changing application.yml to application.properties in src/test/resources, the issue was fixed.

Change database schema used by Spring Boot

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.
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 local EntityManagerFactory 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.
spring.jpa.properties.hibernate.default_schema=your_scheme
OR
spring:
jpa:
properties:
hibernate.default_schema: your_scheme
With HikariDataSource for example spring.jpa.properties.hibernate.default_schema is ignored and you have to set too
spring.datasource.hikari.schema=your_scheme
spring:
jpa:
properties:
hibernate:
default_schema: your_schema_name
I was hitting error:
Cannot acquire connection from data source
org.postgresql.util.PSQLException: ERROR: unsupported startup parameter: search_path
Solution:
application-xyz_dev.yml
url: jdbc:postgresql://localhost:8080/your_database?search_path=your_scheme&stringtype=unspecified
spring:
jpa:
properties:
hibernate.default_schema: your_scheme

Categories

Resources