【问题标题】:rails 4 PG::ConnectionBad could not connect to server [duplicate]rails 4 PG::ConnectionBad 无法连接到服务器 [重复]
【发布时间】:2016-10-27 13:34:42
【问题描述】:

有很多类似的问题,但都略有不同。

宝石文件:

source 'https://rubygems.org'

gem 'rails', '4.2.6'
gem 'pg', '~> 0.15'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc

group :development, :test do
  gem 'byebug'
  gem 'rspec-rails', '~> 3.4'
end

group :development do
  gem 'web-console', '~> 2.0'
  gem 'spring'
end

Rails 4.2.6 并在命令行中执行 psql 返回:

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"?

在启动 rails 服务器并尝试加载根路径后,在浏览器中也得到了相同的结果。

我发现在命令行中做postgres也不起作用,返回:

postgres does not know where to find the server configuration file.
You must specify the --config-file or -D invocation option or set the PGDATA environment variable.

【问题讨论】:

    标签: ruby-on-rails ruby postgresql


    【解决方案1】:

    Re-posted Answer for Convenience:

    添加:

    如果在 OSX 上,请使用 homebrew 管理您的安装。

    安装 Homebrew:/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

    更新它:brew update

    安装数据库:brew install postgresql

    使用 launchctl 加载:launchctl load -w /usr/local/opt/postgresql/homebrew.mxcl.postgresql.plist

    答案:

    它可以像过时的 PID 文件一样简单。它可能会静默失败,因为您的计算机没有完全完成关机过程,这意味着 postgres 没有删除 PID(进程 ID)文件。 postgres 使用 PID 文件来确保一次只运行一个服务器实例。因此,当它再次启动时,它会失败,因为已经有一个 PID 文件告诉 postgres 服务器的另一个实例已启动(即使它没有运行,它只是没有关闭并删除 PID) .

    1. 要修复它删除/重命名 PID 文件。找到 postgres 数据目录。在使用自制软件的 MAC 上,它是 /usr/local/var/postgres/,其他系统可能是 /usr/var/postgres/

    2. 要确定这是问题所在,请查看日志文件 (server.log)。在最后几行你会看到:FATAL: lock file "postmaster.pid" already exists HINT: Is another postmaster (PID 347) running in data directory "/usr/local/var/postgres"?

    3. 如果是,rm postmaster.pid

    4. 重启你的服务器。在使用launchctl(带有自制软件)的Mac上,以下命令将重新启动服务器。

      launchctl unload /usr/local/opt/postgresql/homebrew.mxcl.postgresql.plist

      launchctl load -w /usr/local/opt/postgresql/homebrew.mxcl.postgresql.plist

    【讨论】:

    • 当您可以将答案标记为重复时,为什么还要复制它?
    • 你是怎么做到的?
    【解决方案2】:

    您可以尝试删除/usr/local/var/postgres/postmaster.pid,然后重新启动postgres
    在 Mac OS 上,您可以使用 lunchy 轻松重启。

    【讨论】:

      【解决方案3】:

      我发现this 问题并在命令行中运行export PGDATA=/usr/local/var/postgres。这让我可以使用postgres 启动 postgres(我什至不知道这是必要的 - 为什么 Rails 不为你做这件事?)然后rake db:create,启动 rails 服务器(在新选项卡中)并一切正常!我不知道这是否是最好的解决方案,但认为值得分享。

      【讨论】:

      • 您的 postgres 服务器必须正在运行。 Rails 不负责启动数据库服务器
      猜你喜欢
      • 2020-08-14
      • 2013-11-15
      • 2013-11-18
      • 2015-01-31
      • 1970-01-01
      • 1970-01-01
      • 2018-09-24
      • 1970-01-01
      相关资源
      最近更新 更多