2016-06-24 48 views
0

有很多類似的問題,但都略有不同。rails 4 PG :: ConnectionBad無法連接到服務器

的Gemfile:

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

也得到了在瀏覽器中相同的原料鐵軌服務器和嘗試後加載根路徑。

我發現,在命令行做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. 

回答

1

我發現this問題,並在命令行運行export PGDATA=/usr/local/var/postgres。這讓我可以用postgres(我甚至不知道這是必要的 - 爲什麼Rails不會爲你做這件事?)然後rake db:create,啓動rails服務器(在一個新標籤中)並且一切正常!我不知道這是否是最好的解決方案,但認爲這是值得分享的。

+1

你的Postgres服務器必須運行。它不負責Rails啓動數據庫服務器 – DennisCastro

1

您可以嘗試刪除/usr/local/var/postgres/postmaster.pid,然後重新啓動postgres
在Mac OS上,您可以使用lunchy輕鬆重新啓動。

0

Re-posted Answer for Convenience:

補充:

如果在OSX管理您的安裝與homebrew

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

更新:brew update

安裝DB: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

+0

爲什麼複製一個答案時,你可以把它標記爲重複? –

+0

你是怎麼做到的? – blnc

相關問題