コレグレーデギネード

WindowsとかUbuntuとかRubyとかRailsとか

PostgreSQL使いたいのにgem install pgできない。

Railsの環境構築したついでにデフォルトのsqlite3から、以前メインで使用していたPostgreSQLの環境にする。

環境

PostgreSQL本体をaptでインストール。

$ sudo apt-get install postgresql

su権限でpostgresユーザーを作成。
postgresのsu権限をもつにはroot権限が必要。

$ sudo su postgres
[sudo] password for username: 
postgres@username:/home/username$

createuser [ユーザ名]で作成。
development環境で、あえてDB用のユーザーを作るのは私的ではない。UbuntuのログインIDと同じにしっちまうwww

postgres@username:/home/username$ createuser username
Shall the new role be a superuser? (y/n) y

GemfileでDBのアダプタがsplite3のところをPostgreSQL用のアダプタ名に変更。
8行目あたり。ちなみにPostgresqlのアダプタは「pg」。
gem 'sqlite3'はコメントアウトで残しておくとエラーが出るので必ず削除。

gem 'pg'

さて、あっさり失敗。

$ bundle install
〜省略〜
Installing pg (0.13.2) 
Errno::EACCES: Permission denied - /home/username/.rvm/gems/ruby-1.9.3-head@rails3/gems/pg-0.13.2/.gemtest
An error occured while installing pg (0.13.2), and Bundler cannot continue.
Make sure that `gem install pg -v '0.13.2'` succeeds before bundling.

sudo gem install pgでも入らなーい。

libpq-devをaptでインストールしてからpgをsudo gem installってか。
参考:ruby on rails - Can't find the 'libpq-fe.h header when trying to install pg gem - Stack Overflow

$ sudo apt-get install libpq-dev
$ sudo gem install pg -v 0.13.2
Building native extensions.  This could take a while...
Successfully installed pg-0.13.2
1 gem installed
Installing ri documentation for pg-0.13.2...
Installing RDoc documentation for pg-0.13.2...

いやーキツかった。

config/database.ymlの編集。sqlite3の設定はコメントアウトか消し。

development:
  adapter: postgresql
  encoding: unicode
  database: sampleapp_development
  pool: 5
#  username: 
#  password:

rakeでデータベース作成。

$ rake db:create RAILS_ENV=development
$ psql -l
                                 List of databases
         Name          |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------------------+----------+----------+-------------+-------------+-----------------------
 postgres              | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | 
 sampleapp_development | username | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | 
 template0             | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres          +
                       |          |          |             |             | postgres=CTc/postgres
 template1             | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres          +
                       |          |          |             |             | postgres=CTc/postgres
(4 rows)

migrateでテーブル作成。

$ rake db:migrate
==  CreateUsers: migrating ====================================================
-- create_table(:users)
NOTICE:  CREATE TABLE will create implicit sequence "users_id_seq" for serial column "users.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "users_pkey" for table "users"
   -> 0.0564s
==  CreateUsers: migrated (0.0567s) ===========================================

rails s 確認OK。やれやれ。

----- 補足 -----

$ rails new projectname -d postgresql

というようにデータベースを指定すると、database.ymlの中身もpostgreSQL用に作成される。一応中身を再度チェックしてみたら、

# PostgreSQL. Versions 8.2 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On Mac OS X with macports:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
development:
  adapter: postgresql
  encoding: unicode
  database: projectname_development
  pool: 5
  username: projectname
  password:

コメントアウトMacOS XWindowsそれぞれのpgのインストール方法が親切に記述してあるんでないの。
でも俺環境はUbuntuだからなー8<