0

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.

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.

2
  • @LaurenzAlbe I tried changing the schema name to lowercase and the issue still persists. The error message is still the same. Commented Oct 20, 2022 at 6:16
  • That was just a guess... Commented Oct 20, 2022 at 6:17

0

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.