PostgreSQL
This is my first encounter with PostgreSQL, but not my first one with a relational database. So I guess this will be pretty easy:
$ sudo apt-get install postgresql $ sudo apt-get install postgresql-client $ sudo apt-get install pgadmin3 $ grails create-app SevenDatabases $ cd SevenDatabases
pgadmin3 is a graphical admin console which is very useful to get a start. Hardest part* to get PostgeSQL up and running was to change the password of the root user "postgres". The following lines did the trick for me:
$ sudo -u postgres psql postgres # \password postgres
Now it is easy to connect to your database through pgadmin3 and create a new user ("grailsdbuser") and database ("mydb") for your Grails application.
If you search for "Grails PostgreSQL" on the net, you'll find a lot of good resources on how to configure your project. For my tests, I use the following DataSource.groovy:
dataSource { pooled = true driverClassName = "org.postgresql.Driver" username = "grailsdbuser" password = "grailsdbuser" } hibernate { cache.use_second_level_cache = true cache.use_query_cache = false cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' } environments { development { dataSource { dbCreate = "create-drop" url = "jdbc:postgresql://localhost:5432/mydb" dialect = org.hibernate.dialect.PostgreSQLDialect } } }
This specifies the jdbc driver and connection, but before you can use it, you have to download the jdbc driver and drop it into your /lib folder.
A first test domain can be created through
$ grails create-domain Book $ grails create-domain Author $ grails create-scaffold-controller sevendatabases.Book $ grails create-scaffold-controller sevendatabases.Author
remember: without a specified package, the create-domain script will create the domain within a package with the same name as the project - 'sevendatabases' in this case. Any following script will need the full name of the domain class - that's why we have to specify 'sevendatabases.Book' for the create-scaffold-controller script.
let's add some properties to the Book.groovy and Author.groovy domain classes:
package sevendatabases class Book { String title String isbn Author author static constraints = { title (maxSize:80) author () isbn (maxSize:13) } String toString() {title} }
package sevendatabases class Author { String name Integer age static hasMany=[books:Book] static constraints = { name(maxSize:80) age() books() } String toString() {name} }
Et voilá! grails run-app will show you a nice Grails application with PostgrSQL support!
* it turned out that changing the password was only a problem because I used the Ubuntu terminal in vmware's unity mode. In this mode, the virtual machine displays all Ubuntu windows mixed with your other windows applications. Unfortunately, this mode has some problems with special characters like the backslash '\'. And hence I've got the problem to type in the password command...
No comments:
Post a Comment