Create schema if does not exist by using spring Jpa with hibernate

I’m working on spring boot 2 application and trying to establish connection with postgresql database with configuring hikari datasource and spring Jpa.

I’m succeed in that and i’m using as update so it is creating table if not exists, but only the problem is it is throwing an exception when schema does not exist


GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement
Caused by: org.postgresql.util.PSQLException: ERROR: schema "test_schema" does not exist

I configured everything manually through config class

Config Class

public HikariDataSource dataSource() {

    HikariConfig config = new HikariConfig();


    return new HikariDataSource(config);

public Properties additionalProps() {
    Properties jpaProps = new Properties();

    jpaProps.put(hbDialect, "org.hibernate.dialect.PostgreSQLDialect");
    jpaProps.put(autoDDL, "update");
    jpaProps.put(showSql, true);
    jpaProps.put(formatSql, true);

    return jpaProps;

public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    return factory;

public JpaTransactionManager transactionManager(EntityManagerFactory factory) {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    return transactionManager;

How would i make this to create schema if not exist?

I saw a similar question here and tried everything no luck so far similar-question (i don’t wanna use flyway db)

Here is the documentation hibernate-doc, but it’s not clear how add this property to create schema javax.persistence.schema-generation.database.action


Adding this property worked and created schema if not exist here

jpaProps.put("javax.persistence.create-database-schemas", true);

The JPA variant of hibernate.hbm2dll.create_namespaces. Specifies whether the persistence provider is to create the database schema(s) in addition to creating database objects (tables, sequences, constraints, etc). The value of this boolean property should be set to true if the persistence provider is to create schemas in the database or to generate DDL that contains “CREATE SCHEMA” commands. If this property is not supplied (or is explicitly false), the provider should not attempt to create database schemas.

Leave a Reply

Your email address will not be published. Required fields are marked *