{"id":7151,"date":"2012-09-05T10:54:43","date_gmt":"2012-09-05T05:24:43","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=7151"},"modified":"2016-12-19T15:16:41","modified_gmt":"2016-12-19T09:46:41","slug":"postgresql-with-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/postgresql-with-grails\/","title":{"rendered":"PostgreSQL with Grails"},"content":{"rendered":"<p>Currently in <a title=\"Grails Case Study\" href=\"http:\/\/www.tothenew.com\/success-stories\/mat.se\">my Grails project<\/a> I am using PostgreSQL database so I thought to share my knowledge with everyone. I am using it on Linux operating system. I am mentioning all the steps that I followed to integrate PostgreSQL with Grails .<\/p>\n<p><strong><br \/>\nStep 1:<\/strong> <strong>Install PostgreSQL on your system<\/strong><br \/>\nTo install postgreSql<\/p>\n<p>[java]<br \/>\nsudo apt-get install postgresql<br \/>\n[\/java]<\/p>\n<p>By default the username is postgres<\/p>\n<p>For changing password:<\/p>\n<p>[java]sudo passwd postgres[\/java]<\/p>\n<p>It will ask for new password. Give any password eg: newPassword<\/p>\n<p>[java]su postgres    \/\/switch to the user postgres with password newPassword<br \/>\npsql[\/java]<\/p>\n<p><strong><br \/>\nStep 2: Download PostgreSQL<\/strong><\/p>\n<p>Download latest <a href=\"http:\/\/jdbc.postgresql.org\/\"> PostgreSQL JDBC <\/a>driver and copy it into your application&#8217;s lib folder. For example: myProject\/lib\/postgresql9.1.216.jdbc3.jar<\/p>\n<p><strong><br \/>\nStep 3: Change DataSource file settings<\/strong><\/p>\n<p>[java]<br \/>\ndataSource {<br \/>\n    pooled = true<br \/>\n    driverClassName = &#8220;org.postgresql.Driver&#8221;<br \/>\n    username = &#8220;postgres&#8221;<br \/>\n    password = &#8220;newPassword&#8221;<br \/>\n    dialect = org.hibernate.dialect.PostgreSQLDialect<\/p>\n<p>}<br \/>\nhibernate {<br \/>\n    cache.use_second_level_cache = true<br \/>\n    cache.use_query_cache = true<br \/>\n    cache.provider_class=&#8217;org.hibernate.cache.EhCacheProvider&#8217;<br \/>\n}<\/p>\n<p>environments {<br \/>\n    development {<br \/>\n        dataSource {<br \/>\n            url=&#8221;jdbc:postgresql:\/\/localhost:5432\/appName&#8221;<br \/>\n            dbCreate = &#8220;create-drop&#8221;<br \/>\n            driverClassName = &#8220;org.postgresql.Driver&#8221;<br \/>\n            username = &#8220;postgres&#8221;<br \/>\n            password = &#8220;newPassword&#8221;<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p><strong>Step 4: Create database <\/strong><\/p>\n<p>[java]sudo -u postgres createdb <databaseName>[\/java]<\/p>\n<p>eg: <em>sudo -u postgres createdb myDB<\/em><\/p>\n<p>[java]su postgres   \/\/switch to the user postgres with password newPassword[\/java]<\/p>\n<p>Type \\l to show all databases in postgreSql<\/p>\n<p><em>postgres=# \\l<\/em><\/p>\n<p>[java]<br \/>\npsql -U postgres -d myDB    \/\/ now you are connected with your application<br \/>\n\\d                         \/\/ list all the tables in that database [\/java]<\/p>\n<p>View details of a table structure:<\/p>\n<p>[java]\\d tablename[\/java]<\/p>\n<p>eg: <em>postgres=# \\d customer<\/em><\/p>\n<p><strong><em>Some useful postgreSQL commands:<\/em><\/strong><br \/>\nCreate database<\/p>\n<p>[java]<br \/>\nsudo -u postgres createdb <databaseName>   [\/java]<\/p>\n<p>Drop database<\/p>\n<p>[java]dropdb myDB       [\/java]<\/p>\n<p>Access database<\/p>\n<p>[java]psql mydb         [\/java]<\/p>\n<p>Show databases<\/p>\n<p>[java]postgres=#\\l             [\/java]<\/p>\n<p>Get help<\/p>\n<p>[java]postgres=#\\h           [\/java]<\/p>\n<p>Dump database<\/p>\n<p>[java]pg_dump myDB > db.out   [\/java]<\/p>\n<p>Reload database<\/p>\n<p>[java]psql -d database -f db.out <\/p>\n<p>[\/java]<\/p>\n<p>Disconnect from psql:<\/p>\n<p>[java]<br \/>\npostgres=#\\q [\/java]<\/p>\n<p><strong><br \/>\nNote:<\/strong> In my project I got problem while using &#8220;user&#8221; table name because user is a keyword in PostgreSQL so I changed the name of user table. Instead of changing domain name I just mentioned the table name corresponding to User domain in mapping closure.<br \/>\nAlso, one should keep in mind that postgresql maintains id in sequence unlike mysql. It means that the id generator generates id in continous sequence for all the tables instead of generating from 1 for each table.<br \/>\n<em>Hope this will help you.<\/em> \ud83d\ude42<br \/>\nGunpreet Bedi<br \/>\ngunpreet@intelligrape.com<\/p>\n<p><a href=\"https:\/\/twitter.com\/gunpreet_ginny\">https:\/\/twitter.com\/gunpreet_ginny<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Currently in my Grails project I am using PostgreSQL database so I thought to share my knowledge with everyone. I am using it on Linux operating system. I am mentioning all the steps that I followed to integrate PostgreSQL with Grails . Step 1: Install PostgreSQL on your system To install postgreSql [java] sudo apt-get [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":49},"categories":[7],"tags":[4843,4840,260,942],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7151"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=7151"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7151\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=7151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=7151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=7151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}