はじめに
RailsアプリをHerokuにデプロイしようとするもSQLiteのためデプロイできず、、
仕方なくSQLite⇒Postgresに移行するため、pgのGemをインストールしようとしたところエラー発生。
そもそもPostgresをサーバに入れてなかったりとか、yum使ったせいで古いバージョンだったりとかでエラー吐きまくりでしたが、結局Postgres 11入れてDatabase設定すれば解決しました。
解決手順
PostgreSQLを導入
標準で設定されているyumのリポジトリを使ってしまうと、バージョンが古いため、pgのGemをインストール時にエラーが出ます。
新しいバージョンをリポジトリの指定を付けて導入します。
※間違ってたらすみません。
1 2 3 |
sudo yum install https://yum.postgresql.org/11/redhat/rhel-6.10-x86_64/pgdg-centos11-11-2.noarch.rpm sudo yum install https://yum.postgresql.org/11/redhat/rhel-6.10-x86_64/postgresql11-server-11.0-1PGDG.rhel6.x86_64.rpm sudo yum install https://yum.postgresql.org/11/redhat/rhel-6.10-x86_64/postgresql11-devel-11.1-1PGDG.rhel6.x86_64.rpm |
パスを通します。(下記はvimで編集してます。)
1 |
vim /home/[ユーザ名]/.bash_profile |
下記を追加してください。
1 2 3 |
# postgres export PATH="/usr/pgsql-11/bin/:${PATH}" export PGDATA="/var/lib/pgsql/11/data:${PATH}" |
反映します。
1 |
source /home/[ユーザ名]/.bash_profile |
postgresユーザーのパスワードを設定します。
1 |
sudo passwd postgres |
データベースを初期化して起動します。(文字コードはutf-8にしてます。)
1 2 3 |
su - postgres initdb --no-locale --encoding=utf-8 pg_ctl start |
Railsへのpg導入
config/database.ymlを編集します。
下記はローカル環境も本番環境もPostgresとしてます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
default: &default adapter: postgresql encoding: utf8 pool: 5 # ローカル用 username: [ユーザ名] password: [パスワード] host: localhost development: <<: *default database: [DB名] test: <<: *default database: [DB名] production: <<: *default # 本番用 database: **** username: **** password: **** host: **** |
Gemfileにpgを追加します。
1 |
gem 'pg' |
インストールします。
1 |
bundle install |
ちなみに、herokuへのデプロイ後は”heroku run rails db:migrate”を忘れずに。
【おまけ】bundle install時のエラー
Postgresが入ってないときとか、パス通ってないときのエラー
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
current directory: /opt/rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/pg-1.1.4/ext /opt/rbenv/versions/2.4.2/bin/ruby -r ./siteconf20190203-1732-zs92q6.rb extconf.rb checking for pg_config... no No pg_config... trying anyway. If building fails, please try again with --with-pg-config=/path/to/pg_config checking for libpq-fe.h... no Can't find the 'libpq-fe.h header *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options. Provided configuration options: --with-opt-dir --without-opt-dir --with-opt-include --without-opt-include=${opt-dir}/include --with-opt-lib --without-opt-lib=${opt-dir}/lib --with-make-prog --without-make-prog --srcdir=. --curdir --ruby=/opt/rbenv/versions/2.4.2/bin/$(RUBY_BASE_NAME) --with-pg --without-pg --enable-windows-cross --disable-windows-cross --with-pg-config --without-pg-config --with-pg_config --without-pg_config --with-pg-dir --without-pg-dir --with-pg-include --without-pg-include=${pg-dir}/include --with-pg-lib --without-pg-lib=${pg-dir}/lib To see why this extension failed to compile, please check the mkmf.log which can be found here: /opt/rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/extensions/x86_64-linux/2.4.0-static/pg-1.1.4/mkmf.log extconf failed, exit code 1 Gem files will remain installed in /opt/rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/pg-1.1.4 for inspection. Results logged to /opt/rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/extensions/x86_64-linux/2.4.0-static/pg-1.1.4/gem_make.out An error occurred while installing pg (1.1.4), and Bundler cannot continue. Make sure that `gem install pg -v '1.1.4' --source 'https://rubygems.org/'` succeeds before bundling. In Gemfile: pg |
バージョン古いエラー
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
current directory: /opt/rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/pg-1.1.4/ext /opt/rbenv/versions/2.4.2/bin/ruby -r ./siteconf20190203-1835-1qvvycj.rb extconf.rb checking for pg_config... yes Using config values from /usr/pgsql-9.6/bin/pg_config checking for libpq-fe.h... no Can't find the 'libpq-fe.h header *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options. Provided configuration options: --with-opt-dir --without-opt-dir --with-opt-include --without-opt-include=${opt-dir}/include --with-opt-lib --without-opt-lib=${opt-dir}/lib --with-make-prog --without-make-prog --srcdir=. --curdir --ruby=/opt/rbenv/versions/2.4.2/bin/$(RUBY_BASE_NAME) --with-pg --without-pg --enable-windows-cross --disable-windows-cross --with-pg-config --without-pg-config --with-pg_config --without-pg_config --with-pg-dir --without-pg-dir --with-pg-include --without-pg-include=${pg-dir}/include --with-pg-lib --without-pg-lib=${pg-dir}/lib To see why this extension failed to compile, please check the mkmf.log which can be found here: /opt/rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/extensions/x86_64-linux/2.4.0-static/pg-1.1.4/mkmf.log extconf failed, exit code 1 Gem files will remain installed in /opt/rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/pg-1.1.4 for inspection. Results logged to /opt/rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/extensions/x86_64-linux/2.4.0-static/pg-1.1.4/gem_make.out An error occurred while installing pg (1.1.4), and Bundler cannot continue. Make sure that `gem install pg -v '1.1.4' --source 'https://rubygems.org/'` succeeds before bundling. Either install an older version of this gem or upgrade your database to at least PostgreSQL-9.2. |