Nov 30, 2024
bind. address already in use
Shorten the time it takes to kill a background process.
If you’ve been developing with Postgres locally, you’ve probably run into this annoying error:
failed to bind port 0.0.0.0:5432/tcp: bind: address already in use
How I’ve been solving this problem in the past is by using lsof
to find the PID of the process that’s using the port.
Then, I can kill the process using the PID. You might need to run this command as root.
lsof -i tcp:5432
This will show you the PID of the process that’s using the port.
kill -9 <PID>
Now, you can run your Postgres server again.
It works, but I recently learned you could shorten it to one command:
lsof -ti :5432 | xargs kill -9
What’s happening here?
- You don’t need to specify the tcp protocol, because it’s the default.
- The additional
-t
option makeslsof
return just the PID. - The
| xargs
part uses the pipe operator to pass the output oflsof
toxargs
. xargs kill -9
reads the PIDs and builds a single command to terminate the processes.
PS. This’ll nuke any process on whatever port you provide without asking questions, so double-check what’s running before you pull the trigger.