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:

After this, create a file, named Procfile, with the following:

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:

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:

And in the configuration file application.conf  we set the variables that will read the values from the environment variables in Heroku:

If you liked or you found useful this post, please, go and share it!

 

Can’t connect to database with play framework and Heroku?
Tagged on:             

3 thoughts on “Can’t connect to database with play framework and Heroku?

  • September 27, 2015 at 7:32 pm
    Permalink

    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.

  • December 14, 2015 at 6:21 pm
    Permalink

    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 🙂

    • March 7, 2016 at 4:48 pm
      Permalink

      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!

Comments are closed.