LoginSignup
16
12

More than 5 years have passed since last update.

rake db:migrateしようとするとPG::ConnectionBad: could not connect to server: Connection refusedエラーで怒られる(solved)

Last updated at Posted at 2017-08-28

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が起動していなかったこととデータベースが用意されていなかった。

16
12
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
16
12