LoginSignup
0
0

More than 5 years have passed since last update.

Heroku上でScalikeJDBCからPostgreSQLを使う際のメモ

Last updated at Posted at 2019-03-21

やること

以下の設定をどこかに記述する

  Try {
    sys.env("DATABASE_URL")
  } match {
    case Success(dbUrl) => {
      val uri = new java.net.URI(dbUrl)
      val userName = uri.getUserInfo().split(":")(0)
      val password = uri.getUserInfo().split(":")(1)
      ConnectionPool.singleton(s"jdbc:postgresql://${uri.getHost}:${uri.getPort}${uri.getPath}", userName, password)
    }
    case _ =>
      ConnectionPool.singleton("jdbc:postgresql://localhost:5432/hoge", "user", "password")
  }

解説

Heroku から渡される DATABASE_URL のフォーマットは以下の様になっているが。これだとScalikeJDBCは受け付けてくれない。

postgres://<username>:<password>@<host>/<dbname>

なので上のコードを使って以下のフォーマットに変換してやる必要がある

jdbc:postgresql://<host>:<port>/<dbname>?sslmode=require&user=<username>&password=<password>

参照: https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-java

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0