How to set up different databases per environment in Play 2.0?

Bill picture Bill · May 1, 2012 · Viewed 10.4k times · Source

I'd like my Play app to use different databases for test, local and production (production is Heroku) environments.

In application.conf I have:

db.default.driver=org.postgresql.Driver 

%dev.db.default.url="jdbc:postgresql://localhost/foobar" 
%test.db.default.url="jdbc:postgresql://localhost/foobar-test" 
%prod.db.default.url=${DATABASE_URL} 

This doesn't seem to work. When I run play test or play run, all DB access fails with:

 Configuration error [Missing configuration [db.default.url]] (Configuration.scala:258) 

I have a few questions about this:

  • In general, I'm a little confused about how databases are configured in Play: it looks like there's plain db, db.[DBNAME] and db. [DBNAME].url and different tutorials make different choices among those. Certain expressions that seem like they should work (e.g. db.default.url = "jdbc:..." fail with an error that a string was provided where an object was expected).

  • I've seen other people suggest that I create separate prod.conf, dev.conf and test.conf files that each include application.conf and then contain DB-specific configuration. But in that case, how do I specify what database to use when I run test from the Play console?

  • Is the %env syntax supposed to work in Play 2?

  • What's the correct way to specify an environment for play test to use?

Answer

James Ward picture James Ward · May 1, 2012

In Play 2 there aren't different config environments. Instead you just set or override the config parameters in the conf/application.conf file. One way to do it is on the play command line, like:

play -Ddb.default.driver=org.postgresql.Driver -Ddb.default.url=$DATABASE_URL ~run

You can also tell Play to use a different config file:

play -Dconfig.file=conf/prod.conf ~run

For an example Procfile for Heroku, see:
https://github.com/jamesward/play2bars/blob/scala-anorm/Procfile

More details in the Play Docs:
http://www.playframework.org/documentation/2.0/Configuration