1

There are A LOT of similar questions but all slightly different.

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 and doing psql in the command line returned:

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

Also got the same in the browser after starting the rails server and attempting to load the root path.

I found that doing postgres in the command line didn't work either, returning:

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.
Yorkshireman
  • 2,194
  • 1
  • 21
  • 36

3 Answers3

2

You can try to remove /usr/local/var/postgres/postmaster.pid and then restart postgres.
On Mac OS you can use lunchy for easy restart.

Aleksey
  • 2,289
  • 17
  • 27
1

I found this issue and ran export PGDATA=/usr/local/var/postgres in the command line. That allowed me to start postgres with postgres (I didn't even know this was necessary - why doesn't Rails do that for you?) and then rake db:create, fired up the rails server (in a new tab) and everything worked! I don't know if this is the best solution but thought it was worth sharing.

Yorkshireman
  • 2,194
  • 1
  • 21
  • 36
0

Re-posted Answer for Convenience:

Added:

Manage your installation with homebrew if on OSX.

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

Update it: brew update

Install DB: brew install postgresql

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

Answer:

It could be as simple as a stale PID file. It could be failing silently because your computer didn't complete the shutdown process completely which means postgres didn't delete the PID (process id) file. The PID file is used by postgres to make sure only one instance of the server is running at a time. So when it goes to start again, it fails because there is already a PID file which tells postgres that another instance of the server was started (even though it isn't running, it just didn't get to shutdown and delete the PID).

  1. To fix it remove/rename the PID file. Find the postgres data directory. On a MAC using homebrew it is /usr/local/var/postgres/, other systems it might be /usr/var/postgres/.

  2. To make sure this is the problem, look at the log file (server.log). On the last lines you will see: FATAL: lock file "postmaster.pid" already exists HINT: Is another postmaster (PID 347) running in data directory "/usr/local/var/postgres"?

  3. If so, rm postmaster.pid

  4. Restart your server. On a mac using launchctl (with homebrew) the following commands will restart the server.

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

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

Community
  • 1
  • 1
blnc
  • 4,384
  • 1
  • 28
  • 42