Search code examples
scalaamazon-redshiftslick

How to set up a Redshift connection with Slick?


I would like to set up a connection on an Amazon Redshift instance from Scala with Slick.

Which driver should I use and how can I set it up with sbt and Slick?


Solution

  • We can use Amazon's Redshift driver for Java and include it to the build.sbt (see the release note for the current version):

    resolvers ++= Seq(
      "Redsfhit" at "http://redshift-maven-repository.s3-website-us-east-1.amazonaws.com/release"
    )
    
    libraryDependencies += "com.amazon.redshift" % "redshift-jdbc42" % "1.2.10.1009"
    

    Let's also include Slick dependencies:

    libraryDependencies ++= Seq(
      "com.typesafe.slick" %% "slick"          % "3.2.3",
      "org.slf4j"          %  "slf4j-nop"      % "1.6.4",
      "com.typesafe.slick" %% "slick-hikaricp" % "3.2.3"
    )
    

    One way to configure the connection with Slick consists in using a typesafe config. Let's create application.conf in src/main/resources/ with your Redhsift settings:

    my_redshift {
      url = "jdbc:redshift://examplecluster.abc123xyz789.us-west-2.redshift.amazonaws.com:5439/dev"
      user = my_user
      password = my_password
      driver = com.amazon.redshift.jdbc.Driver
      connectionPool = disabled
      keepAliveConnection = true
    }
    

    And we can finally load this configuration from Scala as such (my_redshift is the root you've chosen in the typesafe config):

    import slick.jdbc.PostgresProfile.api._
    
    val db: Database = Database.forConfig("my_redshift")