Thoughts about tech, programming, and more.

Server is already running

Problem 1: Rails Server Already Running

From your project directory:

cat tmp/pids/server.pid

(or project_path/tmp/pids/server.pid if not in project directory)

This will return a process ID (PID), not a port number. To stop the Rails server:

kill -9 $(cat tmp/pids/server.pid)

If the process is no longer running but the PID file remains, simply remove it:

rm tmp/pids/server.pid

Problem 2: PostgreSQL Connection Refused

connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?

This error occurs when PostgreSQL isn't running or wasn't properly shut down before the machine restarted, leaving a stale postmaster.pid file.

Solution for Apple Silicon Macs (M1/M2) with Homebrew-installed PostgreSQL:

First, check if PostgreSQL is actually running:

brew services list | grep postgresql

If it shows as "started" but you still can't connect, remove the stale PID file:

rm /opt/homebrew/var/postgresql@14/postmaster.pid

(Note: Replace postgresql@14 with your version, or use just postgres depending on your installation)

Then restart PostgreSQL:

brew services restart postgresql

Before removing the PID file, it's always good to verify PostgreSQL isn't actually running and check the logs:

# Check if postgres processes are running
ps aux | grep postgres

# View the log file
tail -n 50 /opt/homebrew/var/log/postgresql@14.log

(Again, adjust the path based on your PostgreSQL version)

Subscribe to Daniel Lemky

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe