45
39

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[*Rails*] 開発環境用DBをSQLite3からPostgreSQLに変更する 

Last updated at Posted at 2015-01-05

はじめに

Ruby on Railsチュートリアル第3章 ほぼ静的なページの作成の演習3の、開発環境のDBをSQLite3からPostgreSQLに変更する演習についてやってみたのでまとめる。
環境は以下の通り。

  • CentOS6.5(Vagrant)
  • Ruby2.0.0
  • Rails4.0.5

手順

Gemfileは以下。(Railsチュートリアルに記載してあるものと同じ)

Gemfile
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0

gem 'rails', '4.0.5'
gem 'pg', '0.15.1'

group :development, :test do
  gem 'rspec-rails', '2.13.1'
end

group :test do
  gem 'selenium-webdriver', '2.35.1'
  gem 'capybara', '2.1.0'
end

gem 'sass-rails', '4.0.5'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'

group :doc do
  gem 'sdoc', '0.3.20', require: false
end

group :production do
  gem 'rails_12factor', '0.0.2'
end

config/database.ymldevelopmentを以下のように修正。

config/database.yml
development:
  adapter: postgresql
  host: localhost
  encoding: utf8
  database: psgr_db
  username: postgres
  password: postgres
  pool: 5
  timeout: 5000

以下実行。

$ bundle update

こんなエラーが出たら、

An error occurred while installing pg (0.15.1), and Bundler cannot continue.
Make sure that `gem install pg -v '0.15.1'` succeeds before bundling.

ひとまずエラーのアドバイスに従って以下実行。

$ gem install pg -v '0.15.1'

もう一回bundle updateするとこんなエラーが出るので

Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.

/home/vagrant/.rvm/rubies/ruby-2.0.0-p598/bin/ruby 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.
...省略...

以下を実行。その後bundle updateしてもエラーは出なくなる。

$ sudo yum install postgresql-devel

その後以下を実行して、

$ sudo yum install postgresql-server

postgresユーザーのパスワードを変更して、

$ sudo passwd postgres

postgresユーザーに切り替えて

$ su postgres

データベースの初期化。

$ initdb -D /var/lib/pgsql/data

こんなのがバーっと出る。

could not change directory to "/home/vagrant/rails/railsTutorial/sample_app"
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale ja_JP.UTF-8.
The default database encoding has accordingly been set to UTF8.
initdb: could not find suitable text search configuration for locale ja_JP.UTF-8
The default text search configuration will be set to "simple".

fixing permissions on existing directory /var/lib/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 32MB
creating configuration files ... ok
creating template1 database in /var/lib/pgsql/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the -A option the
next time you run initdb.

Success. You can now start the database server using:

postgres -D /var/lib/pgsql/data
or
pg_ctl -D /var/lib/pgsql/data -l logfile start

下の方に書いてあるやつを実行。logfileつけたら怒られたので以下実行。

$ pg_ctl -D /var/lib/pgsql/data start

もとのユーザーに戻ってデータベースを作成してみる。

$ rake db:create RAILS_ENV=development

もう1回postgresユーザーに切り替えて出来てるか確認。
psgr_dbというconfig/database.ymlで指定したDBができてる。

$ su postgres
$ psql -l
                                  List of databases
   Name    |  Owner   | Encoding |  Collation  |    Ctype    |   Access privileges  
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |
 psgr_db   | postgres | 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)

適当にモデル作ってrake db:migrateやってrails consoleからデータを入れて、DBの中見てみたらちゃんとデータが入っていた。(idが2のデータがコンソールから入れたやつ)

$ su postgres
$ psql psgr_db postgres
$ select * from static_pages;
 id | name  | age |         created_at         |         updated_at         
----+-------+-----+----------------------------+----------------------------
  1 | test  |  20 |                            | 
  2 | test2 |  30 | 2015-01-05 15:27:37.290788 | 2015-01-05 15:27:37.290788
45
39
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
45
39

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?