Rails4.2で作っていたプロジェクトをHerokuで運用していくことになりました。
しかし、さくらvpsやAWSを想定していたこともあり、慣れたMySQLをこれまで使っていました。
そこでMySQLからPostgresへ移し替えることになりまして、その際起きたトラブルのメモを残します。
MacにPostgresをインストール
設定変更
# MySQL. Versions 5.0+ are recommended.
#
# Install the MYSQL driver
# gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
# gem 'mysql2'
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
#
まず↑のあたりは削除しました。
default: &default
# adapter: mysql2
adapter: postgresql
encoding: utf8
pool: 5
# username: root
username: hoge
password:
# socket: /tmp/mysql.sock
development:
<<: *default
database: hoge_development
test:
<<: *default
database: hoge_test
production:
<<: *default
database: hoge_production
password: <%= ENV['HOGE_DATABASE_PASSWORD'] %>
#gem 'mysql2'
gem 'pg'
gem 'rails_12factor', group: :production
あんまり変更するところはなかったですが、↑の様にしました。
rails s をしたらspringが怒るトラブル
Springを使っていたこともあり、rails sが通らなくなりました。
$ rails s
=> Booting WEBrick
=> Rails 4.2.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
bin/rails:6: warning: already initialized constant APP_PATH
/Users/hoge/bin/rails:6: warning: previous definition of APP_PATH was here
Usage: rails COMMAND [ARGS]
The most common rails commands are:
generate Generate new code (short-cut alias: "g")
console Start the Rails console (short-cut alias: "c")
server Start the Rails server (short-cut alias: "s")
dbconsole Start a console for the database specified in config/database.yml
(short-cut alias: "db")
new Create a new Rails application. "rails new my_app" creates a
new application called MyApp in "./my_app"
In addition to those, there are:
destroy Undo code generated with "generate" (short-cut alias: "d")
plugin new Generates skeleton for developing a Rails plugin
runner Run a piece of code in the application environment (short-cut alias: "r")
All commands can be run with -h (or --help) for more information.
↓とかしてみましたが、うまくいかず。
$ rake rails:update:bin
ここをみたりしましたが、rbenvを入れ替えるといいとか。
めんどくさいので、とりあえずspringを消しました。
$ bin/spring binstub --remove --all
# GemfileからSpringの部分をコメントアウトして
$ bundle install
これで rails s は通るようになりました。
rails db でのエラー
$ rails db
psql: FATAL: role "hoge" does not exist
macのアカウント名であるhogeが、roleに登録されてないよってエラーが出ます。
Postgresに入る
postgresアカウントになる
$ sudo su - postgres
or
$ sudo -u postgres -i
入る
postgres$ psql
psql (9.4.1, server 9.4.0)
Type "help" for help.
roleと権限を作成
現状確認
postgres-# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication | {}
roleと権限作成
Macで使うアカウントと同じ名前で行けば問題ないと思います。
postgres=# create role hoge createdb replication superuser createrole login
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication | {}
hoge | Superuser, Create role, Create DB, Replication | {}
ここではhogeというロールを作成して、create db, replication, superuser, create role login(表示はされていませんが)の権限を与えています。
とはいえ、railsで使うだけなのであれば、create db, replication, loginで事足りるんじゃないかなと思います。(一旦その権限でテストしてみましたが、問題ない感じがします)
パスワードを忘れた場合
なおパスワードを忘れた場合はこちらから
railsからpostgresに接続
rake db:create
まずhoge_developmentデータベースを作らないといけません。
$ rake db:create
もしcreatedbの権限を与えてないとエラーがでます。
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"utf8", "pool"=>5, "username"=>"hoge", "password"=>nil, "database"=>"hoge_development"}
PG::InsufficientPrivilege: ERROR: permission denied to create database
: CREATE DATABASE "matcha_test" ENCODING = 'utf8'
Postgresで確認
$ sudo -u postgres -iPassword:
postgres$ psql
psql (9.4.1, server 9.4.0)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
--------------------+----------+----------+---------+-------+-----------------------
hoge_development | hoge | UTF8 | C | C |
hoge_test | hoge | UTF8 | C | C |
↑できてます!
一応、チョクではいってみます↓
$ psql -U hoge -d hoge_development
psql (9.4.1, server 9.4.0)
Type "help" for help.
hoge_development=>
入れました!
rails db
railsのコマンドでやってみます。
$ rails dbpsql (9.4.1, server 9.4.0)
Type "help" for help.
hoge_development=>
いいですね。
データが入るか確認
migrateやpryで見てみるといいです。
$ rake db:migrate
$ rails c
References
- http://stackoverflow.com/questions/6710654/how-do-you-easily-change-from-sqlite-to-postgresql-in-rails
- http://sasuke.main.jp/postgrescmd.html
- https://www.postgresql.jp/document/9.3/html/app-psql.html
- https://www.postgresql.jp/document/9.3/html/role-attributes.html
- http://d.hatena.ne.jp/iriya_0624/20130424/1366783019
- http://stackoverflow.com/questions/11919391/postgresql-error-fatal-role-username-does-not-exist