LoginSignup
1
2

More than 5 years have passed since last update.

PostgreSQLでHerokuへプッシュがうまくいかないときに試したこと

Last updated at Posted at 2018-08-05

以前の記事で、Herokuへのデプロイ時にPostgresql関連のエラーが出て苦戦したことがありました。

こちら↓
Herokuデプロイ時のエラー記録

そのときはPostgresqlの代わりにmysqlで代用したことでheroku openさせることができました。

今回はHerokuでPostgresqlを使い、同じようなエラーは出たけど、それも解決できたので投稿します。

既出ですが自分の備忘録も兼ねてめもめも。( 'u')φ....

作業環境

  • Rails 5.0.1
  • Ruby 2.5.0
  • mysql2 0.4.10 (ローカル環境)  

エラー内容:Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded.

(エラー発生までの経緯)Gemfileにpgを追加

まず、自分の作業経緯を振り返る。

1:Gemfileにpgを追加

Gemfile
gem 'pg', group: :production

group: :productionで本番環境はPostgresqlを使うよう指定しています。

2: bundle installを実行

3:database.ymlに以下の記述を追加

database.yml
production:
  <<: *default
  adapter: postgresql
  encoding: unicode
  pool: 5

※もともとのproduction:~の記述箇所を上の内容に書き換える
※ymlファイルでは改行場所やスペースなどでエラーが出ることがあるので、既存の記述と見比べるなどして特に気を付ける。



4:この状態でherokuにプッシュまで行う

git add .

git commit -m "コメント"

heroku create(初回の場合のみ)

git push heroku master ここでエラー発生!



5 :発生したpgエラー

remote:        Gem::LoadError: 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).

対処:pgのバージョンを0.19.0に指定

参考記事。バージョン指定がポイントでした。
https://qiita.com/Hijiri-K/items/51d53c0512a2ef455da6

Gemfileでpgの記述をバージョン指定して、以下のように書き換えました。

Gemfile
gem 'pg', '~> 0.19.0', group: :production

これでbundle install

これで再度herokuにプッシュまで行う

git add .

git commit -m "コメント"

git push heroku master うまくいきました!!



これで
heroku run rake db:migrate実行

heroku openでアプリを開く、で終了。


ちなみに
bundle installしたときの画面で

Fetching pg 0.19.0 (was 1.0.0)
Installing pg 0.19.0 (was 1.0.0) with native extensions

pgは、もともと1.0.0だったけど0.19.0にバージョン下げていたようですがうまくいきました。
バージョンの違いによるエラー?

まとめ

成功した手順

  1. pgのバージョンを0.19.0指定で記述
  2. database.yml書き換え
  3. git addとcommit

でPostgresqlを使ってherokuにプッシュが成功。


ちなみに私はこのあとのheroku run rake db:migrate実行で別のエラーが出たので、
別記事に書いておきました。

Hatenaでもエラー備忘録書いてます。
http://nojishio-notes.hatenablog.com/archive/category/%E3%82%A8%E3%83%A9%E3%83%BC%E5%82%99%E5%BF%98%E9%8C%B2
Qiitaでも記事増やしていきたいです。
ここまで読んでくださり、ありがとうございました^^

1
2
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
1
2