Postgresql the database system is starting up

踩坑 postgresql。

问题描述

执行rake db: migrate时,中断,再次执行rake db: migrate时,报错:

psql: could not connect to server: Connection refused
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?

解决过程

重启posgresql:

brew services restart postgresql

显示:

Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

再次运行:

rake db:migrate

报错:

psql: could not connect to server: Connection refused
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?

改成手动启动:

postges -D /usr/local/var/postgres/

报错:

FATAL:  pre-existing shared memory block (key 5432001, ID 65536) is still in use
HINT:  If you're sure there are no old server processes still running, remove the shared memory block or just delete the file "postmaster.pid".

切换到/usr/local/var/postgres 下,删除postmaster.pid 文件,然后重启:

postgres -D /usr/local/var/postgres/

重启后,再次运行:

rake db:migrate

报错:

PG::ConnectionBad: FATAL:  the database system is starting up

Google后,尝试修改了postgresql.conf文件,将hot_standby 设置为on,重启Postgresql,继续执行rake db:migrate, 依然报错:

PG::ConnectionBad: FATAL:  the database system is starting up

最后的解决方法:

简单粗暴:删除文件夹 /usr/local/var/postgres,卸载并重装PostgreSQL,重新导入数据。