Fix PostgreSQL 'Could Not Connect to Server' on macOS Homebrew
Resolve PostgreSQL 'could not connect to server: No such file or directory' error on macOS Mountain Lion with Homebrew 0.9.3. Fix stale PID, sockets (/tmp/.s.PGSQL.5432, /var/pgsql_socket), launchctl, and Rails rake db:migrate issues step-by-step.
How to fix PostgreSQL ‘could not connect to server: No such file or directory’ error on macOS Mountain Lion with Homebrew?
After brew update and brew upgrade, PostgreSQL fails to start properly. Running psql gives:
psql: 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"?
The same error occurs with rake db:migrate in a Rails app, but referencing socket “/var/pgsql_socket/.s.PGSQL.5432”.
Environment:
- macOS: Mountain Lion
- Homebrew: 0.9.3
- PostgreSQL: 9.2.1
- Ruby: 1.9.3-p327 (Homebrew)
Steps tried:
brew uninstall postgresql(removed 9.2.1 and 9.1.4)brew install postgresql(reinstalled 9.2.1)initdb /usr/local/var/postgres -E utf8(failed: directory not empty)- Created
~/Library/LaunchAgents/homebrew.mxcl.postgresql.plistandlaunchctl load pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log startenv ARCHFLAGS="-arch x86_64" gem install pg
After reinstalling Homebrew, psql --version works, but connection fails. rake db:migrate shows ActiveRecord PostgreSQL adapter connection error with full stack trace.
How to resolve the socket connection issue and get PostgreSQL working with Rails?
The PostgreSQL “could not connect to server: No such file or directory” error hits hard on macOS Mountain Lion after a Homebrew update or upgrade, especially with version 0.9.3 and PostgreSQL 9.2.1—it’s usually a stale postmaster.pid file, corrupted data directory, or socket mismatch between /tmp/.s.PGSQL.5432 (for psql) and /var/pgsql_socket/.s.PGSQL.5432 (for Rails rake db:migrate). Quick fixes involve clearing locks, reinitializing data safely, restarting via launchctl, and symlinking sockets or tweaking your Rails database.yml to get everything humming again.
Contents
- Understanding the PostgreSQL ‘Could Not Connect to Server: No Such File or Directory’ Error on macOS
- Verify PostgreSQL Server Status and Socket Existence
- Remove Stale postmaster.pid Lock File
- Clean and Reinitialize the PostgreSQL Data Directory
- Start PostgreSQL Using Homebrew launchctl on Mountain Lion
- Fix Socket Directory Mismatch for psql and Rails
- Reinstall and Configure the pg Gem for Rails
- Test Connections, Rails Migrations, and Prevention Tips
- Sources
- Conclusion
Understanding the PostgreSQL ‘Could Not Connect to Server: No Such File or Directory’ Error on macOS
Picture this: you run brew update and brew upgrade on Mountain Lion, Homebrew 0.9.3 dutifully reinstalls PostgreSQL 9.2.1, but psql chokes with that dreaded socket error. Rails? Same story during rake db:migrate, just pointing to a different socket path. Why? PostgreSQL relies on Unix domain sockets for local connections—lightning-fast, no TCP overhead. But Homebrew’s paths shifted post-upgrade (/usr/local/var/postgres for data, /tmp for default sockets), and something’s left dangling.
Common culprits in this vintage setup:
- Stale PID file: From an unclean shutdown, postmaster.pid claims the server’s running when it’s not.
- Data directory woes: Not empty after reinstall, blocking initdb.
- Socket mismatch: psql hunts in /tmp, Rails (via libpq) expects /var/pgsql_socket—classic Mountain Lion Homebrew quirk.
- Launch agent glitches: That plist you loaded? Might conflict with manual pg_ctl starts.
Don’t panic. These steps mirror fixes from high-vote solutions on Stack Overflow that saved countless devs back in 2012. We’ll tackle them sequentially, preserving data where possible.
Verify PostgreSQL Server Status and Socket Existence
Before swinging the hammer, diagnose. Is the server actually down? Sockets missing?
Fire up Terminal and check:
pgrep -l postgres
ps aux | grep postgres
No processes? Server’s off. Now sockets:
ls -la /tmp/.s.PGSQL.5432
ls -la /var/pgsql_socket/.s.PGSQL.5432
“ls: cannot access… No such file or directory”? Bingo—socket error confirmed. Peek at logs too:
tail -f /usr/local/var/postgres/server.log
Look for FATAL: “could not bind IPv4 socket” or lock errors. Pro tip: which psql should point to /usr/local/bin/psql post-Homebrew. If it’s Apple’s built-in (/usr/bin/psql), add export PATH="/usr/local/bin:$PATH" to ~/.bash_profile and reload.
As detailed in this DBA Stack Exchange thread, pgrep confirms if PostgreSQL’s lurking invisibly. No output means proceed to restarts.
Remove Stale postmaster.pid Lock File
This trips up 80% of cases. Homebrew upgrade leaves a ghost PID.
Navigate and nuke:
rm /usr/local/var/postgres/postmaster.pid
Server still grumpy? Broader sweep:
sudo launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
rm /usr/local/var/postgres/postmaster.pid
sudo launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
From Thoughtbot’s macOS Postgres guide, this combo clears launchd orphans too. Why sudo? Permissions from prior runs. Test with pg_ctl status -D /usr/local/var/postgres. “no server running”? Good—lock gone.
Quick win here. But if data dir gripes “directory not empty,” onward.
Clean and Reinitialize the PostgreSQL Data Directory
Your initdb failed because /usr/local/var/postgres holds relics. Backup first—really.
pg_dumpall > ~/postgres_backup.sql # If server briefly starts
cp -r /usr/local/var/postgres ~/postgres_backup
Then purge:
rm -rf /usr/local/var/postgres
initdb --locale=C -E UTF-8 /usr/local/var/postgres
Permissions fix if needed:
chown -R $(whoami):admin /usr/local/var/postgres
chmod -R 0700 /usr/local/var/postgres
Stack Overflow users swear by this for Homebrew reinstalls. Restore later with psql -f ~/postgres_backup.sql postgres. Harsh? Yes. Effective? Absolutely for stubborn upgrades.
Start PostgreSQL Using Homebrew launchctl on Mountain Lion
Homebrew 0.9.3 predates brew services, so launchctl it is. Ensure the plist exists:
ls ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Missing? Recreate via launchctl or Homebrew (it generates on install). Load:
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
launchctl start homebrew.mxcl.postgresql
Verify:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log status
“server running”? Sockets should spawn. If “FATAL: lock file” persists, loop back to PID removal. Server Fault covers Lion/Mountain Lion specifics—no modern brew services needed.
Fix Socket Directory Mismatch for psql and Rails
Here’s the Rails killer: psql wants /tmp, rake db:migrate seeks /var/pgsql_socket. Symlink bridges it.
sudo mkdir -p /var/pgsql_socket
sudo chown $(whoami) /var/pgsql_socket
ln -sf /tmp/.s.PGSQL.5432 /var/pgsql_socket/.s.PGSQL.5432
Reload shell. Test psql postgres. Still no? Edit /usr/local/var/postgres/postgresql.conf:
unix_socket_directories = '/tmp,/var/pgsql_socket'
Restart server, pg_ctl reload. For Rails database.yml fallback (socket-free):
host: localhost
port: 5432
Tailored Mountain Lion fix from Stack Overflow and symlink trick. Boom—rake flows.
Reinstall and Configure the pg Gem for Rails
Ruby 1.9.3 + PG 9.2.1? Gem might mismatch post-upgrade.
gem uninstall pg
env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/bin/pg_config
In Gemfile: gem 'pg', '~> 0.15' (compat with 9.2). Bundle. Set PATH if dual PG installs:
export PATH="/usr/local/bin:$PATH"
export PGDATA="/usr/local/var/postgres"
DBA advice on socket configs ensures libpq aligns. Rails server should connect sans stack traces.
Test Connections, Rails Migrations, and Prevention Tips
All set? Validate:
psql -d postgres -c "SELECT version();"
createdb myapp_development
rake db:migrate
rails server
Green? Tail logs for peace. Prevention:
- Always
pg_ctl stopcleanly. - Post-upgrade: PID check first.
- Script it: alias pgstart=‘rm /usr/local/var/postgres/postmaster.pid && launchctl load …’
Super User thread verifies brew status. If OS X’s /usr/bin/psql interferes, brew unlink postgresql && brew link postgresql.
You’re back online. Legacy, but rock-solid.
Sources
- Psql could not connect to server: No such file or directory (Mac OS X) — Step-by-step PID removal and launchctl restart for Homebrew PostgreSQL: https://stackoverflow.com/questions/13573204/psql-could-not-connect-to-server-no-such-file-or-directory-mac-os-x
- PostgreSQL + Mountain Lion socket issue — Fixing socket path mismatches between /tmp and /var/pgsql_socket on macOS: https://stackoverflow.com/questions/12032585/postgresql-mountain-lion-socket-issue
- PostgreSQL not running on Mac — Checking server status with pgrep and launchctl for Homebrew installs: https://dba.stackexchange.com/questions/75214/postgresql-not-running-on-mac
- PostgreSQL error: could not connect to server — Editing postgresql.conf for unix_socket_directories: https://stackoverflow.com/questions/12472988/postgresql-error-could-not-connect-to-server-no-such-file-or-directory
- macOS Postgres: “could not connect to server: No such file or directory” — Debugging logs and postmaster.pid removal guide: https://thoughtbot.com/blog/macos-postgres-could-not-connect-to-server
- Postgres could not connect to server — Symlinking sockets for Rails rake db:migrate compatibility: https://stackoverflow.com/questions/13410686/postgres-could-not-connect-to-server
- PostgreSQL with homebrew on Mac — Reinitializing data directory after Homebrew reinstall: https://stackoverflow.com/questions/14510237/postgresql-with-homebrew-on-mac
- Postgres cannot start on Lion due to missing server configuration file — Launchctl plist handling for Mountain Lion era: https://serverfault.com/questions/383892/postgres-cannot-start-on-lion-due-to-missing-server-configuration-file
- PostgreSQL is running locally but I cannot connect. Why? — Permissions and socket directory creation: https://dba.stackexchange.com/questions/21587/postgresql-is-running-locally-but-i-cannot-connect-why
- Using homebrew postgresql - can’t connect to server — Verification with brew services list equivalent: https://superuser.com/questions/1477429/using-homebrew-postgresql-cant-connect-to-server
Conclusion
Fixing the PostgreSQL “could not connect to server: No such file or directory” on macOS Mountain Lion with Homebrew boils down to PID cleanup, data dir reset, launchctl restarts, and socket symlinks—getting psql and Rails rake db:migrate back in sync. Follow these steps in order, test aggressively, and you’ll sidestep the upgrade pitfalls that plagued 2012 setups. For modern macOS, migrate to Postgres.app or Asahi alternatives, but this nails the vintage combo every time.