Rails4エラー:Could not load ‘active_record/connection_adapters/pg_adapter’の対処法

記事公開日:
最終更新日:

This post is also available in: English-US (英語)

Rails4で以下のようなエラーに遭遇したときの対処法のメモです。
私の場合は、ローカルのdevelopment環境ではsqlite3を使用していて、herokuのproduction環境でpostgresqlを利用しようとした時のエラーです。

LoadError: Could not load 'active_record/connection_adapters/pg_adapter'. Make s
ure that the adapter in config/database.yml is valid. If you use an adapter othe
r than 'mysql', 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter ge
m to the Gemfile.

1.development/test環境とproduction環境で使うデータベースを分ける

Gemfileに以下のように記述して、development/test環境とproduction環境で使うデータベースを分けると便利です。

#Gemfile
#development/test環境
group :development, :test do
  gem 'sqlite3'
end

#production環境
group :production do
  gem 'pg',             '0.17.1'
end

もちろん、忘れずに bundle install してください。

bundle install

2.database.ymlに利用するデータベースを定義する

production環境で使うデータベースをpostgresqlに変更しています。

default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

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

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: db/test.sqlite3

production:
  adapter: postgresql
  database: db/production.pg

About
Amelt.net,LLCの創業者で、費用対効果の高い統合webマーケティングによりビジネスパートナーとして継続的にサポート。詳しいより。ブログの更新情報TwitterLinkedIn、またRSSfeedlyにてお知らせしていますのでフォローよろしくお願い致します。