RailsアプリをHerokuにデプロイしようとしたら、PostgreSQL関連でApplication Errorになったので、備忘録として残しておきます。
環境
エラー発生時の環境は以下の通りです。
| 対象 | バージョン |
|---|---|
| OS | Windows10 Pro |
| シェル | Windows Subsystem for Linux |
| Ruby | 2.5.0 |
| Rails | 5.1.4 |
エラー内容
rails newで作成したプロジェクトのGemfileにpgを追加してデプロイ。
Gemfile
group :production do
gem 'pg'
end
するとデプロイは成功しますが、Application Errorが発生。
2018-01-15T06:17:41.119766+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=XXX-YYY-ZZZ.herokuapp.com request_id=47bc2299-d9ee-4b14-ab3f-574e9f2af93b fwd="118.103.29.194" dyno= connect= service= status=503 bytes= protocol=https
これだけでは原因が分からないので、
rails consoleで詳細のエラーを見てみるとPostgreSQL関連でエラーが出ていました。
2018-01-15T06:16:30.567689+00:00 app[web.1]: /app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/connection_specification.rb:188:in `rescue in spec': Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). (Gem::LoadError)
解決方法
実際にインストールされているpgのバージョンを確認してみると1.0.0となっていました。
このバージョンが怪しいと考え、0.20.0を試しに設定して、再デプロイ!
Gemfile
group :production do
gem 'pg', '0.20.0'
end
すると正常に起動![]()
HerokuのPostgreSQLに関するページでは特にバージョン指定はなかったので盲点でした。