background info
rake db:migrate
を実行しようとすると以下のようなエラーが出てくる:
PG::ConnectionBad: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
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?
attempt1: 再インストール
$ gem uninstall pg
$ bundle install
を試みたが特に変化はなし。
attempt2: database.ymlの書き換え
default: &default
host: localhost
を足すと直るかもしれないときき実行したが特に変化なし。
attempt3: そもそもpostgresql実行していないのでは?
How to start PostgreSQL server on Mac OS X?を参考にしながらpg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
でマニュアル実行。ちなみにマニュアルストップの方法はpg_ctl -D /usr/local/var/postgres stop -s -m fast
。
$ pg_ctl -D /usr/local/var/postgres status
pg_ctl: server is running (PID: 52599)
/usr/local/Cellar/postgresql/9.6.4/bin/postgres "-D" "/usr/local/var/postgres"
動いている!
そこでrake db:migrate
を実行してみると今度は以下のようなエラーが:
rake aborted!
ActiveRecord::NoDatabaseError: FATAL: database "mynewapp_development" does not exist
PG::ConnectionBad: FATAL: database "mynewapp_development" does not exist
データベースがそもそもないと怒られているみたいなのでrake db:create
を実行してデータベースを作成する。
$ rake db:create
Created database 'mynewapp_development'
Created database 'mynewapp_test'
$rake db:migrate
rake db:migrate
== 20170828031040 CreatePosts: migrating ======================================
-- create_table(:posts)
-> 0.0028s
== 20170828031040 CreatePosts: migrated (0.0028s) =============================
参照: rails 4 change database from sqlite3 to pgsql
エラーの原因
単純にpostgresqlが起動していなかったこととデータベースが用意されていなかった。