Server is 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 port number.
kill -9 port_number
(insert port number from above)
Unsafe way if the above doesn't work
connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
The problem is due to PostgreSQL not properly closing before the machine was restarted and then a straggling postmaster.pid
file is left, making the machine think that the port is in use.
Remove the file on an M1 Mac (or M2... any Apple Silicon) with the following command:
rm /opt/homebrew/var/postgres/postmaster.pid
Then restart PostgreSQL (this only works if PostgreSQL was installed using Homebrew).
brew services restart postgresql
By now this has happened enough times after restarting my Mac that I'm confident that I can remove the file, but if you're unsure it's always good to double check /opt/homebrew/var/log/postgres.log
.