Friday, October 11, 2013

Play2 and Postgres

Play2 and Postgres


H2 is great database and it integrates well to Play2 during development. I use Ebean ORM and it just works. When I decided to try local Postgres I found some differences, but basically everything went fine.

More on developing with H2 from here.

http://www.playframework.com/documentation/2.2.x/Developing-with-the-H2-Database

Installing Postgres


Postgres installation options for Mac are here

http://www.postgresql.org/download/macosx/

I selected Postgres.app, which was perfect for me, since it's easy to operate.

http://postgresapp.com/

Just to make sure all goes well I also added it to path in .profiles file

  • export PG_HOME=/Applications/Postgres.app/Contents/MacOS/bin 
  • export PATH="$PG_HOME:$PATH" 

Testing connection was easy

  • psql -h localhost

But I also installed induction.app for browsing content

http://inductionapp.com/


Getting driver


I used Postgres 9.3 and wanted to use latest driver. Driver was easy to find from maven central.

project/build.scala needed single new dependency for jdbc driver

  "postgresql" % "postgresql" % "9.1-901.jdbc4",

Here's more of setting up your database

http://www.playframework.com/documentation/2.2.x/JavaDatabase

Setting up connection


I could have hacked existing application.conf, but decided to have separate conf/postgres-local.conf for Postgres database settings

  • db.default.driver=org.postgresql.Driver
  • db.default.url="jdbc:postgresql://localhost/nikkijuk"
  • db.default.user="nikkijuk"
  • db.default.password=""
nikkijuk = $USER, so please replace with your local account name

You can find more of JDBC settings from here

http://www.playframework.com/documentation/2.2.x/SettingsJDBC

Starting app


For deploying Play2 apps to production one shouldn't ever use "run". I wanted to override settings from command line and use Postgres as 2nd development database. So; "run" is ok.

  • play -Dconfig.file=conf/postgres-local.conf ~run

If you want to know more of overriding settings, please see

http://www.playframework.com/documentation/2.2.x/ProductionConfiguration

Datatype generation


I had in Entities some strange stuff like maximum size of numeric identifier.

@NotNull
@Column(unique = true)
// @Size(min = 2, max = 64)
public long processId;


"@Size(min = 2, max = 64)" definiton generated fields with type bigint(64), which went thru on H2, but were inappropriate on Postgres. Commenting out extra annotations solved problems here.

Development process


I found out that Ebean and H2 are unbeatable during development. Changes on data model (Entity classes) were automatically detected, sql was generated, and database model changed according model.

With Postgres there isn't as tight integration. My process is thus, that I first develop all with H2, and then let Play2 to run SQL sentences that set up Postgres. This works fine to me, and at the end I know that I have two very good databases on my service locally.

Further steps


Get your app to Cloud. Heroku is here a good option, since it supports natively both Play2 and Postgres.


No comments: