Justin Dearing
Justin Dearing

Reputation: 14928

How do I get hibernate to execute CREATE DATABASE if necessary for postgresql when hibernate.hbm2ddl.auto is set to create

I am using spring-roo, gwt and hibernate to make a website. We are using the in memory database HyperSonic, but I am trying to switch to postgres.

Everything works fine if I used the jdbc3 driver. The only problem is I have to separately execute the CREATE DATABASE statement outside of hibernate before it will create the tables via [hibernate.hbm2ddl.auto](http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-optional) being set to create. Is their another option I can set to cause hibernate to do the CREATE DATABASE if necessary?

Upvotes: 0

Views: 1564

Answers (1)

Sean
Sean

Reputation: 10206

Not really. You have to do a CREATE DATABASE using a template database (normally template1). The normal sequence of events is:

  1. Connect to template1 database
  2. Execute CREATE DATABASE newdb;
  3. Reconnect to the newdb;
  4. Begin issuing DDL statements.

If you're interested in more information, look in to the specifics of how PostgreSQL completes the CREATE DATABASE and you'll understand why.

Upvotes: 2

Related Questions