herokuのRun the app locallyでbundle exec rake db:create db:migrateできなくてはまった話

ここに至る手順

bundle exec rake db:create db:migrate

を行う前にthese instructionsを踏んでpostgresqlのインストールを行わなければならない。

行わないと

could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"ruby-getting-started_development"}
could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?

というエラーが出てきてハマる。つか、はまった。
(ドキュメントが英語だからって読まないで進めようとするから……)

うし、公式のコピペだー。

sudo apt-get install postgresql
which psql
psql

そして、psqlでエラーが発生した。

psql: FATAL:  role "nukisashineko" does not exist

ロールの設定が必要という・・・。


PostgreSQLの初期導入のエラーにもてあそばれ、格闘すること2時間余り、
UbuntuでPostgreSQLをインストールからリモートアクセスまでの手順 - Qiita
FreeBSD + PostgreSQL 9.3.2 外部からの接続を許可する - Symfoware
などを見て、やっと設定が完成する。

解決法

最終的にherokuのbundle exec rake db:create db:migrateを行うために必要な手順。
ちな、全て行わないとバグる

1. postgresqlのインストール

sudo apt-get install postgresql

2. ロール(ユーザみたいなもん)の作成

sudo su - postgres
createuser ruby-getting-started  --createdb --login   --pwprompt
Enter password for new role: 
Enter it again: 

と入力して、postgresの権限で、パスワード持ちのユーザーを作成。

3. ログイン時の認証制限の緩和

/etc/postgresql/{version}/main/pg_hba.confの90行付近のlocalに対するpeer認証を外して、無条件でlocalなら無条件で入れるようにする。
(peer認証 == 現在のシェルログイン者と同一のロールでなければログイン拒否をする機構)

# "local" is for Unix domain socket connections only
local   all             all                                    peer
# "local" is for Unix domain socket connections only
local   all             all                                    trust

と変更。で、/etc/init.d/postgresql restart再起動して、設定を反映

4. railsのDBの設定の変更

heroku/ruby-getting-started/config/database.ymlを編集。 development,testの配下にそれぞれ、 username: ruby-getting-started password: password を追記する。 コメントを全部消すとこんな感じ。

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
development:
  <<: *default
  database: ruby-getting-started_development
  username: ruby-getting-started
  password: password
test:
  <<: *default
  database: ruby-getting-started_test
  username: ruby-getting-started
  password: password
production:
  <<: *default
  database: ruby-getting-started_production
  username: ruby-getting-started
  password: <%= ENV['RUBY-GETTING-STARTED_DATABASE_PASSWORD'] %>

で、これで、bundle exec rake db:create db:migrateすると、とりあえず動く。 commitしても、とりあえず動く、heroku側ではproductionの設定を使用しているようだ。