flaurida
flaurida

Reputation: 501

Cannot kill Postgres process

I keep trying to kill a PostgreSQL process that is running on port 5432 to no avail. Whenever I type sudo lsof -i :5432, I see something like the below:

COMMAND  PID     USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
postgres 587 postgres    4u  IPv6 0x218f97e9af5d0303      0t0  TCP *:postgresql (LISTEN)
postgres 587 postgres    5u  IPv4 0x218f97e9ae0f6c63      0t0  TCP *:postgresql (LISTEN)

I then try to kill the process 587 in this example with sudo kill -9 587, but then another process automatically restarts on the same port! I have tried killing it on activity monitor as well to no avail. Please help?

Thanks, Laura

Upvotes: 45

Views: 53131

Answers (9)

jam
jam

Reputation: 1293

my suffering:

sudo lsof -PiTCP -sTCP:LISTEN

enter image description here

(replace 54011 with your PID)

ps -f -p 54011

enter image description here

pg_ctl status -D /opt/homebrew/var/postgres

enter image description here

 pg_ctl -D /opt/homebrew/var/postgres stop

but failed for me :(

enter image description here


my issue is with the launchctl of macos, restarting the homebrew.mxcl.postgresql service, but the suggested solutions for removing the service didn't work

launchctl bootout gui/501/homebrew.mxcl.postgres

only removes the service until the next login

and editing

vi /var/db/com.apple.xpc.launchd//disabled.501.plist

by removing the lines

<key>homebrew.mxcl.postgres</key>
<false/>

didn't effect anything.

So I finally removed the postgres binary of the deamon

 rm -r /opt/homebrew/Cellar/postgresql@14

the service homebrew.mxcl.postgres is still there but no process.


the issue was in the LaunchAgent, so I removed the file

rm ~/Library/LaunchAgent/homebrew.mxcl.postgresql.plist

or in root

rm /Library/LaunchAgent/homebrew.mxcl.postgresql.plist

and

launchctl bootout gui/501/homebrew.mxcl.postgres

finally worked for me, no more homebrew.mxcl.postgres entry in launchctl list, after relogin.

Upvotes: 2

Yoshihisa Okada
Yoshihisa Okada

Reputation: 33

For those who's still struggling.

You can give it a try with the steps below

brew install pstree // enables you to see structuralized tree process
pstree | grep postgre

Remember the path of the parent task

Example)
/opt/homebrew/var/postgres

Then you remove the .pid file located at the directory of the parent task

rm -rf  <path of the parent task>/postmaster.pid
kill <PID of the parent task>

I hope it works for you too :)

Upvotes: 0

epurdy
epurdy

Reputation: 261

I had this issue and what I did to resolve it was first run

    brew services

to see a list of the services that home brew is running. In my case the service was called 'postgresql@12' so I had to run

    brew services stop postgres@12

Upvotes: 13

tn2000
tn2000

Reputation: 758

Try to run this command:

sudo pkill -u postgres

Upvotes: 9

Moebius
Moebius

Reputation: 6508

If you installed postgres using brew, this command might be what you are looking for :

brew services stop postgres

Upvotes: 46

Daniel
Daniel

Reputation: 2288

I had this issue trying to stop postgres. I was unable to use pg_ctl stop. I installed postgress using brew.

I eventually stumbled upon this post which solved the issue for me.

Upvotes: 0

Steven Shi
Steven Shi

Reputation: 1231

list your postgres pid:

pg_ctl status -D /usr/local/var/postgres
pg_ctl: server is running (PID: 715)

force kill it..

kill -9 715

Upvotes: 7

Stanislav Kr.
Stanislav Kr.

Reputation: 554

I have 9.5 and 9.6 installed, so

sudo su - postgres

/Library/PostgreSQL/9.6/bin/pg_ctl -D /Library/PostgreSQL/9.6/data stop

9.5 started ...

/Library/PostgreSQL/9.5/bin/pg_ctl -D /Library/PostgreSQL/9.5/data stop

Upvotes: 14

l&#39;L&#39;l
l&#39;L&#39;l

Reputation: 47169

The process is restarting likely because it's spawned from a launchd daemon. You can try finding it and killing it through the launchctl command:

$ launchctl list

To kill a process you would:

$ launchctl kill

Upvotes: 2

Related Questions