PG::ConnectionBad - 无法连接到服务器:连接在 Mac OS X 上被拒绝

PG::ConnectionBad - could not connect to server: Connection refused on Mac OS X

安装所需的 ruby 版本后:rvm install 2.7.1 并安装所有 gems, 我打算用 rails db:create 创建数据库,但出现此错误:

could not connect to server: Connection refused
  Is the server running on host "127.0.0.1" and accepting
  TCP/IP connections on port 5442?
Couldn't create 'development' database. Please check the configuration.
rails aborted!
PG::ConnectionBad: could not connect to server: Connection refused
  Is the server running on host "127.0.0.1" and accepting
  TCP/IP connections on port 5442?

我的database.yml

default: &default
  adapter: postgresql
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 15 } %>
  host: <%= ENV.fetch("DATABASE_HOST") { '127.0.0.1' } %>

development:
  <<: *default
  database: development

test:
  <<: *default
  database: test

production:
  <<: *default
  database: production

似乎与 postgres 有关。 它的版本是:postgres (postgreSQL) 12.4

pg gem 版本为 1.2.3

重新启动 postgresql 没有帮助

看来我需要创建一个新的 PostgreSQL 数据库集群。 首先,您需要删除 /usr/local/var/

中的 postgres 目录

您可以使用此代码 - cd /usr/local/var/ && rm -rf postgres

之后 - 创建新的 postgres 文件夹 mkdir postgres(在 /usr/local/var 内)

并使用 -

初始化新 PostgreSQL 数据库集群的创建

cd && initdb --locale=C -E UTF-8 /usr/local/var/postgres

最后一件事,重启 postgres - brew services restart postgresql