0

I have this hibernate.cfg.xml:

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">org.postgresql.Driver</property>

        <property name="connection.url">
            jdbc:postgresql://localhost/DatabaseName?createDatabaseIfNotExist=true
                &amp;useUnicode=yes&amp;characterEncoding=UTF-8
        </property>

        <property name="connection.username">postgres</property>
        <property name="connection.password">password</property>
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">5</property>
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.PostgreSQL9Dialect</property>
        <!-- Disable the second-level cache -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</property>
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>
        <mapping class="com.main.User"/>
    </session-factory>
</hibernate-configuration>

which is supposed to create both tables (for the User entity and the database DatabaseName). However, it doesn't create a database and fails with an error on this line:

sessionFactory = configuration.buildSessionFactory();

What can I do to make it autocreate database titled DatabaseName?

parsecer
  • 4,758
  • 13
  • 71
  • 140
  • Creating a database from your application is a really bad idea. Only a superuser can create a new database, and letting your application connect to the database as the superuser is a security risk you should not take. –  Jan 14 '20 at 17:36
  • I don't see a `createDatabaseIfNotExist` *connection parameter* listed in the PostgreSQL JDBC [**documentation**](https://jdbc.postgresql.org/documentation/head/connect.html#connection-parameters), so why do you believe such a parameter exists *for PostgreSQL*? --- I don't see `useUnicode` and `characterEncoding` either. Are you attempting to use connection parameter from a different database on PostgreSQL? Connection parameters are very driver-specific. You can't use parameters from a difference driver. – Andreas Jan 14 '20 at 17:50
  • @a_horse_with_no_name Yet Hibernate can create tables inside a database? Does it not require a superuser? Security concerns aside, I'd still be curious as to how to make it work. – parsecer Jan 14 '20 at 17:50
  • @Andreas It was proposed in this: https://stackoverflow.com/a/31422939/4759176 answer – parsecer Jan 14 '20 at 17:51
  • @parsecer That's a MySQL connection string, not a PostgreSQL connection string. Connection string syntax is driver-specific. – Andreas Jan 14 '20 at 17:53
  • 1
    @parsecer *"I'd still be curious as to how to make it work"* Recommendation: Don't! Not for PostgreSQL. Create the database manually. You can still have the program create the tables. – Andreas Jan 14 '20 at 17:55
  • For creating a table no superuser is required - although if you really want to make your application secure, don't grant your application user the privileges to create (and thus also drop) tables. MySQL's "database" is actually what a schema is in Postgres - and regular users can create schemas without problems –  Jan 14 '20 at 17:56

1 Answers1

1

To create database you have to create is manually you can use IDEs for that or cmd to create your DB

And To create Tables you can use create in place of update

 <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">create</property>

It will create a schema and then you will use an update for the next startup if you don`t want to recreate your tables