Deploying a Play framework application to Heroku is really easy, normally you just set Heroku as a git remote branch and push the your code there. Simple. If your app has a database, you will have to configure it for the different environments, so that you don’t get that awful “Cannot connect to database” message.
On Saturday I deployed a Play framework application with Slick 3.0.1 to Heroku using PostgreSQL as database. PostgreSQL became one of my favorite relational databases since I started using it on my thesis project with the PostGIS map extension. PostgreSQL is very stable and easy to use, moreover because it comes with the pgAdmin III UI interface that makes your database administration really straightforward. Heroku also has also an integration with PostgreSQL and they offer you also PostgreSQL hosting on the cloud with Heroku Postgres, so for me, perfect match.
Configuration
So lets get into it. The first thing we have to do is ensure you have the Postgres driver dependency in your application’s built.sbt file, in my case:
1 |
libraryDependencies += "org.postgresql" % "postgresql" % "9.4-1201-jdbc41" |
After this, create a file, named Procfile, with the following:
1 |
web: target/universal/stage/bin/myapp -Dhttp.port=${PORT} -Dplay.evolutions.db.default.autoApply=true |
Where myapp is the your application’s name. This file will indicate Heroku’s dynos how should they start your app. Important note: I haven’t included here anything about the DATABASE_URL variable. When you create an application in Heroku that has a Postgres database, it will create database on Heroku’s cloud with the DATABASE_URL environment variable pointing to it:
1 |
postgres://username:password@host:port/db |
Play usually generates de JBDC URL from this information, but Slick doesn’t really like it and it doesn’t work. In order to get it working, define the following environment variables in Heroku, always adapting them to your needs:
1 2 3 |
JDBC_DATABASE_URL = jdbc:postgresql://host:port/db JDBC_DATABASE_USERNAME = username JDBC_DATABASE_PASSWORD = password |
And in the configuration file application.conf we set the variables that will read the values from the environment variables in Heroku:
1 2 3 |
db.default.url=${?JDBC_DATABASE_URL} db.default.username=${?JDBC_DATABASE_USERNAME} db.default.password=${?JDBC_DATABASE_PASSWORD} |
If you liked or you found useful this post, please, go and share it!
I run into the same problem and came up with the same solution. The problem is Heroku can and will change the value of DATABASE_URL without any warning and your custom variables will be incorrect.
Hi Alberto,
Thanks for such nice and useful information. But still I found issue in your article for slick related configuration. With mentioned configuration it won’t work.
I tried it with same config you mentioned here but it was not working. Correct configuration is as below,
slick.dbs.default.driver=”slick.driver.PostgresDriver$”
slick.dbs.default.db.properties.driver=”org.postgresql.Driver”
slick.dbs.default.db.url=${?JDBC_DATABASE_URL}
slick.dbs.default.db.user=${?JDBC_DATABASE_USERNAME}
slick.dbs.default.db.password=${?JDBC_DATABASE_PASSWORD}
slick.dbs.default.db.connectionTimeout=5 seconds
slick.dbs.default.db.connectionPool = “disabled”
slick.dbs.default.db.properties.connectionPool = “HikariCP”
Thanks 🙂
Hi Nishan,
for me it was working with the configuration in the post but thanks a lot for your new configuration and for sharing it here. It may help other that run into the same issue 🙂
I really appreciate it. Thanks!