PostgreSQL "Could not connect to server: No such file or directory" occurs when the client cannot find the Unix domain socket file needed to connect locally. Common causes include PostgreSQL not running, mismatched socket paths between client and server, permission issues, or leftover postmaster.pid files blocking startup. Fixing requires restarting the server, verifying socket locations, and checking permissions.
This error occurs when the PostgreSQL client (psql, libpq, application drivers) attempts to connect to a local PostgreSQL server via Unix domain socket but cannot locate the socket file. On Unix/Linux systems, PostgreSQL creates a socket file in a configured directory (default /var/run/postgresql or /tmp) with a predictable name like .s.PGSQL.5432. When the client attempts to connect without specifying a host (or with host=localhost on non-Windows systems), it looks for this socket file. If the file does not exist, you get this error. The error itself does not tell you why the file is missing—only that it cannot be found. The underlying cause is almost always one of: PostgreSQL server is not running, the server failed to start properly and never created the socket file, the client and server have different socket directory configurations, the socket file was deleted or the directory was removed, or the socket file exists but the client lacks permissions to access it.
The most common cause is that the server is not running. Check if the postgres process is active.
# Check for postgres processes:
pgrep -l postgres
# or:
ps aux | grep postgres
# On Linux with systemd:
sudo systemctl status postgresql
# On macOS with Homebrew:
brew services list | grep postgres
# On other systems:
sudo service postgresql statusIf no postgres process is running, the server needs to be started. Proceed to the next step.
If the server is not running, start it cleanly.
# On Linux with systemd:
sudo systemctl start postgresql
# On older Linux systems:
sudo service postgresql start
# or:
sudo /etc/init.d/postgresql start
# On macOS with Homebrew:
brew services start postgresql
# or for a specific version:
brew services start postgresql@14
# On manual installations:
sudo -u postgres /usr/lib/postgresql/14/bin/pg_ctl start -D /var/lib/postgresql/14/mainWait a few seconds for the server to fully start before attempting to connect.
If the server will not start, a stale postmaster.pid file may be blocking startup. Remove it and try again.
# First, verify no postgres processes are running:
pkill postgres
# Find and remove the postmaster.pid file:
sudo find / -name "postmaster.pid" 2>/dev/null
# Common locations:
sudo rm /var/lib/postgresql/14/main/postmaster.pid
sudo rm /usr/local/var/postgres/postmaster.pid # macOS Homebrew
sudo rm /tmp/postmaster.pid
# Then restart:
sudo systemctl start postgresql
# or on macOS:
brew services restart postgresqlAlways verify that no postgres processes are running before removing postmaster.pid, as removing it while the server is running can corrupt the database.
Verify the socket directory exists and the postgres user can create files in it.
# Check what socket directory PostgreSQL is configured to use:
sudo -u postgres psql -c "SHOW unix_socket_directories;"
# If the above fails (because the server is not running), check the configuration file:
grep -n "unix_socket_directories" /etc/postgresql/14/main/postgresql.conf
# Default locations:
# Linux (Debian/Ubuntu): /var/run/postgresql
# Linux (manual): /tmp
# macOS (Homebrew): /tmp
# Verify the directory exists:
ls -ld /var/run/postgresql
ls -ld /tmp
# If the directory does not exist, create it:
sudo mkdir -p /var/run/postgresql
sudo chown postgres:postgres /var/run/postgresql
sudo chmod 2777 /var/run/postgresqlThe directory must be owned by the postgres user and have write permissions (typically 2777 or 755).
After starting PostgreSQL, the socket file should be created in the configured directory.
# Check for the socket file (default port 5432):
ls -l /var/run/postgresql/.s.PGSQL.5432
ls -l /tmp/.s.PGSQL.5432
# List all socket files:
ls -l /var/run/postgresql/
ls -l /tmp/.s.PGSQL*
# Check file permissions:
ls -la /var/run/postgresql/ | grep PGSQLThe socket file should be owned by postgres and world-readable/writable (permissions 0777 or similar). If it does not exist after starting the server, check the PostgreSQL error log.
If the server is running but the client still cannot find it, the client may be looking in the wrong socket directory. Specify the socket path explicitly.
# If server uses /var/run/postgresql but client looks in /tmp:
psql -h /var/run/postgresql -U postgres
# Or set the environment variable:
export PGHOST=/var/run/postgresql
psql
# For applications, set PGHOST in the connection string or DSN:
# PostgreSQL connection string: postgresql://postgres@/var/run/postgresql/postgres
# To verify which socket PostgreSQL is using:
sudo -u postgres psql -c "SHOW unix_socket_directories;"
# To verify which socket the client is looking for:
psql --version
psql -d postgres -h /var/run/postgresql -U postgresIf you need to change the server's socket directory, edit postgresql.conf and restart the server.
On macOS, Homebrew installations sometimes have incomplete or misaligned configurations.
# Check Homebrew PostgreSQL status:
brew services list | grep postgres
# Stop the service:
brew services stop postgresql
# Verify it is stopped:
brew services list | grep postgres
# Restart with a fresh start:
brew services start postgresql
# If problems persist, reinstall:
brew uninstall postgresql
brew install postgresql
# Initialize a fresh database:
rm -rf /usr/local/var/postgres
brewpostgres-setup
brew services start postgresql
# Test the connection:
psql -U $(whoami)Always back up your data before uninstalling.
If the server fails to start or acts unexpectedly, check the logs for clues.
# On Linux with systemd:
sudo journalctl -u postgresql -n 50
# On systems with a separate log file:
tail -50 /var/log/postgresql/postgresql-14-main.log
# On macOS:
log stream --predicate 'eventMessage contains "postgres"' --level debug
# Or check Homebrew log:
tail -50 $(brew --prefix)/var/log/postgres.log
# To see startup messages directly, start the server in foreground:
sudo -u postgres /usr/lib/postgresql/14/bin/postgres -D /var/lib/postgresql/14/mainCommon log messages include permission denied, port already in use, or failed to initialize the data directory. Fix the issue shown in the logs before attempting reconnection.
In containerized environments (Docker, Kubernetes), the socket file exists only within the container. If your application and PostgreSQL are in different containers, you must connect via TCP (host:port) instead of Unix socket. Set the connection string to something like "postgresql://host:5432/dbname" instead of relying on socket defaults. The socket approach only works if your application and database are on the same container or share a volume.
For replication and cluster setups, each PostgreSQL instance has its own socket in its own unix_socket_directories. If you are connecting to a replica or cluster node, ensure you are pointing to the correct socket path for that specific instance.
On systems with AppArmor or SELinux enabled, socket file creation or access may be blocked by policy. Check apparmor logs with sudo dmesg | grep apparmor or SELinux logs with sudo ausearch -m AVC. You may need to update the security policy to allow socket access.
When migrating PostgreSQL to a different system or updating a major version, the socket directory path may change. Always verify unix_socket_directories in the new postgresql.conf and update client connection strings accordingly.
If you customized unix_socket_directories to use the abstract namespace (Linux only), the path starts with "@" but is not a real directory. For example, unix_socket_directories = "@/tmp" means the socket is at "@/tmp/.s.PGSQL.5432" in abstract namespace, and cannot be seen in the filesystem. Connect with the specified path directly: psql -h "@/tmp" -U postgres.
insufficient columns in unique constraint for partition key
How to fix "insufficient columns in unique constraint for partition key" in PostgreSQL
ERROR 42501: must be owner of table
How to fix "must be owner of table" in PostgreSQL
trigger cannot change partition destination
How to fix "Trigger cannot change partition destination" in PostgreSQL
SSL error: certificate does not match host name
SSL error: certificate does not match host name in PostgreSQL
No SSL connection
No SSL connection to PostgreSQL