I'm trying to execute a SQL script on the different schema in spring boot. However, I got the following errors:
org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #2 of class path resource [sql/init.sql]: CREATE TABLE IF NOT EXISTS authorities ( username varchar(255) NOT NULL, authority varchar(50) NOT NULL, CONSTRAINT fk_authorities_users FOREIGN KEY(username) REFERENCES users(username) ); nested exception is org.postgresql.util.PSQLException: ERROR: no schema has been selected to create in
Position: 28
init.sql
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS authorities (
username varchar(255) NOT NULL,
authority varchar(50) NOT NULL,
CONSTRAINT fk_authorities_users
FOREIGN KEY(username)
REFERENCES users(username)
);
CREATE SEQUENCE IF NOT EXISTS sequence_username_id START WITH 500000000;
ALTER TABLE users ALTER COLUMN attempts DROP NOT NULL;
ALTER TABLE users ALTER COLUMN usernameid SET NOT NULL;
ALTER TABLE users ALTER COLUMN usernameid SET DEFAULT nextval('sequence_username_id'::regclass);
ALTER TABLE users ALTER COLUMN externalthirdpartyuserflag SET DEFAULT false;
ALTER TABLE users ALTER COLUMN passwordtemporaryflag SET DEFAULT false;
ALTER TABLE users ALTER COLUMN activated SET DEFAULT false;
COMMIT TRANSACTION;
BaseIntegrationTest.java
. . .
@Value("${dynamic-schema}")
private String dynamicSchema;
protected void executeSqlFile(String sqlFileName) {
jdbcTemplate.execute("SET search_path TO \"" + dynamicSchema + "\"");
jdbcTemplate.execute("grant usage on schema public to public");
jdbcTemplate.execute("grant create on schema public to public");
Resource resource = new ClassPathResource(sqlFileName);
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(resource);
databasePopulator.execute(jdbcTemplate.getDataSource());
}
applications.properties
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.hbm2dll.create_namespaces=true
hibernate.default_schema=${dynamic-schema}
. . .
dataSource.url=jdbc:postgresql://localhost:5432/${dynamic-database}?currentSchema=${dynamic-schema}
dataSource.driverClassName=org.postgresql.Driver
. . .
dataSource.hikari.jdbcUrl=jdbc:postgresql://localhost:5432/postgres?currentSchema=${dynamic-schema}
dataSource.hikari.username=postgres
dataSource.hikari.password=postgres
dataSource.hikari.autoCommit=false
datasource.hikari.schema=${dynamic-schema}
. . .
dynamic-schema=MySchema
AuthDataSourceConfiguration.java
. . .
private Properties getJpaProperties(HibernateProperties hibernateProperties) {
Properties jpaProperties = new Properties();
. . .
jpaProperties.put("javax.persistence.create-database-schemas", true);
return jpaProperties;
}
I have tried these solutions, but it still produces the same errors.
- How can I specify the schema to run an sql file against in the Postgresql command line
- https://dba.stackexchange.com/questions/106057/error-no-schema-has-been-selected-to-create-in
- how to properly specify database schema in spring boot?
I tried changing the schema name to public
and it's working. However, I still haven't figured out why it's not working for another schema name.