LoginSignup
20
16

More than 3 years have passed since last update.

HerokuにRailsアプリをデプロイした時のdatabase.ymlの置き換えについて

Last updated at Posted at 2017-10-28

Ruby on Rails Tutorial 第4版をしていて、pushしたdatabase.ymlの内容と実際の動作が異なっていたのでメモします。

database.ymlにはpgの設定がないのにHerokuで正常動作する

Tutorialに従っていると、database.ymlの中身は以下の様になっているはず。

database.yml
default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

(中略)

production:
  <<: *default
  database: db/production.sqlite3

Gemfileはこんな感じ。

Gemfile
group :production do
  gem 'pg', '0.18.4'
end

database.ymlにはsqlite3の設定しかないのに、デプロイしたHerokuではpgで正しく動作しています。ふしぎです。

HerokuのRubySupportの項にちゃんと説明がありました

Heroku::RubySupportによると、herokuにアップしたdatabase.ymlの内容はすべて無視され、上書きされるということでした。

Rails4.1以上の場合は、以下のようにdatabase.ymlを作成/変更するようです。

  • database.ymlがない場合は、database.ymlを作成する
  • database.ymlがある場合は、database.ymlを置き換える
  • 上のdatabase.ymlはHeroku環境変数のDATABASE_URLの内容をパースして動的に作成される
20
16
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
20
16