Fixing PostgreSQL Connection Error: No such file or directory
When working with PostgreSQL on macOS, you may encounter the error:
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
This typically indicates that your PostgreSQL server isn't running or can't be accessed through the expected Unix socket. Let's explore the common causes and effective solutions.
Common Causes
- Improper shutdown: Force-quitting applications or unexpected system shutdowns
- Stale PID file: A leftover
postmaster.pid
file from a previous session - Multiple PostgreSQL versions: Conflicts between different installations
- Incorrect configuration: Wrong port settings or data directory paths
- Disk space issues: Full storage preventing server startup
Step-by-Step Solutions
1. Check if PostgreSQL is Running
First, verify if PostgreSQL is actually running:
pgrep -l postgres
If no processes are listed, your server isn't running.
2. Remove Stale PID File (Most Common Fix)
The most common solution is to remove the stale postmaster.pid
file and restart PostgreSQL:
rm /usr/local/var/postgres/postmaster.pid
brew services restart postgresql
rm /opt/homebrew/var/postgres/postmaster.pid
brew services restart postgresql
rm /opt/homebrew/var/postgresql@15/postmaster.pid
brew services restart postgresql@15
WARNING
Always stop the PostgreSQL service before removing the PID file:
brew services stop postgresql
# or for specific version
brew services stop postgresql@15
3. Check PostgreSQL Logs
If the above doesn't work, examine the logs for specific error messages:
tail /usr/local/var/log/postgres.log
# or
tail /opt/homebrew/var/log/postgresql@15.log
Look for messages like "FATAL: lock file 'postmaster.pid' already exists" or other startup errors.
4. Verify Disk Space
A full disk can prevent PostgreSQL from starting:
df -H
Clear space if your root drive is near capacity.
5. Check PostgreSQL Configuration
Ensure PostgreSQL is configured to use the standard port (5432):
# Check which port PostgreSQL is using
cd /opt/homebrew/var/postgres
grep "port =" postgresql.conf
If it's using a different port, you can either:
- Change the configuration to use port 5432
- Connect using the specific port:
psql -p 5433
6. Recreate Default Database
Sometimes the default database needs to be recreated:
createdb
psql -h localhost
7. Complete Reinstall (Last Resort)
If all else fails, you may need to completely reinstall PostgreSQL:
brew uninstall --force postgresql
brew install postgresql
brew services start postgresql
Prevention Tips
Always stop PostgreSQL properly:
bashbrew services stop postgresql
Avoid force-quitting applications that might be connected to PostgreSQL
Keep regular backups of important databases
Monitor disk space to prevent storage-related issues
Alternative: Use Docker
Consider using PostgreSQL via Docker to avoid local installation issues:
docker run --name postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres
When to Seek Further Help
If none of these solutions work, you might have:
- File permission issues in PostgreSQL data directory
- Corrupted database files
- Conflicts with other database installations
Check PostgreSQL's official documentation or seek help in community forums with details about your specific setup and error messages from the logs.
INFO
Remember that the exact file paths may vary depending on your PostgreSQL version and installation method. Use brew info postgresql
to find the correct paths for your setup.