问题描述
创建新的 Rails 项目时,运行 db:create 之后创建 database 失败,得到这样的提示:
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
Couldn't create 'demo-4_development' database. Please check your configuration.
rails aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
/Users/david/code/david/demo/bin/rails:9:in `<top (required)>'
/Users/david/code/david/demo/bin/spring:15:in `<top (required)>'
./bin/rails:3:in `load'
./bin/rails:3:in `<main>'
Tasks: TOP => db:create
(See full trace by running task with --trace)
但是运行 brew services list
的话,显示 PostgreSQL 正常运行
解决办法
已经是第二次遇到这个问题了,上一次也花了我一个晚上,但是没有把过程记录下来。这次又花了一个晚上,终于从一篇 Medium文章里找到答案。发一下这篇文章的链接:
Effectively Uninstall and Reinstall PSQL with Homebrew on OSX
如果只是运行 brew uninstall postgresql
是没有作用的,还要以下这几步:
1.先卸载 postgresql
brew uninstall postgresql
2.切换到项目路径以外的路径
cd ~
3.然后删除项目文件
rm -rf rm -rf /usr/local/var/postgres
rm -rf .psql.local .psql_history .psqlrc.local l.psqlrc .pgpass
4.运行 initdb
initdb /usr/local/var/postgres
这时候再回到 Rails 文件夹 运行 rails db:create, 会发现没问题了。