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

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


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


At least in Play 2.1.1 it is possibly to override configuration values with environment variables, if they are set. (For details see: http://www.playframework.com/documentation/2.1.1/ProductionConfiguration)

So you can set the following in your conf/application.conf:

db.default.url="jdbc:mysql://localhost:3306/my-db-name"db.default.url=${?DATABASE_URL_DB}

per default it will use the JDBC-URL defined unless the environment variable DATABASE_URL_DB defines a value for it.So you just set your development database in the configuration and for production or stages you define the environment variable.

But beware, this substitution does NOT WORK if you put your variable reference inside quoted strings:

db.default.url="jdbc:${?DATABASE_URL_DB}"

Instead, just unquote the section to be substituted, for example.

database_host = "localhost"database_host = ${?ENV_DATABASE_HOST}db.default.url="jdbc:mysql://"${?database_host}":3306/my-db-name"

In this example, localhost will be used by default if the environment variable ENV_DATABASE_HOST is not set. (For details see: https://www.playframework.com/documentation/2.5.x/ConfigFile#substitutions)


You can actually still use the Play 1.0 config value naming method, in Play 2, if you, when you load config values, check if Play.isTest, and then prefix the properties you load with 'test.'. Here's a snipped:

def configPrefix = if (play.api.Play.isTest) "test." else ""def configStr(path: String) =  Play.configuration.getString(configPrefix + path) getOrElse     die(s"Config value missing: $configPrefix$path")new RelDb(  server = configStr("pgsql.server"),  port = configStr("pgsql.port"),  database = configStr("pgsql.database"),  user = ...,  password = ...)

And the related config snippet:

pgsql.server="192.168.0.123"pgsql.port="5432"pgsql.database="prod"...test.pgsql.server="192.168.0.123"test.pgsql.port="5432"test.pgsql.database="test"...

Now you don't need to remember setting any system properties when you run your e2e test suite, and you won't accidentally connect to the prod database.

I suppose that you can optionally place the test. values in a separate file, which you would then include at the end of the main config file I think.