46

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

3
  • 4
    Do not use kill -9 on the main PostgreSQL process (the postmaster). There is the danger that some PostgreSQL backend processes don't die imediately, and if a new postmaster is started before all the old processes are gone, you will end up with data corruption. Commented Jun 17, 2017 at 18:57
  • 1
    Laurenz is right. Killing a Postgres process from the command line is a bad idea. You need to properly shut down Postgres (e.g. pg_ctl stop) Commented Jun 17, 2017 at 19:07
  • I have the same problem, first I'm trying to kill a user with pg_terminate_backend, without result, then I do the pg_ctl stop -mf, but with no result, just when I kill a specific session with kill -9 dies, but my server restart Commented Sep 27, 2018 at 14:29

9 Answers 9

47

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

brew services stop postgres
Sign up to request clarification or add additional context in comments.

3 Comments

Error: No available formula with the name "postgres". Did you mean postgrest or postgis?
@J.R.BobDobbs neither, I mean postgres
@J.R.BobDobbs this answer is probably what you need; you need to @ your version. stackoverflow.com/a/73157164/3826315
14

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

Comments

14

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

2 Comments

brew services stop postgresql@15 is what I needed. Thanks!
it says service not found! my version is 14.
12

Try to run this command:

sudo pkill -u postgres

1 Comment

this one worked inxtead of brew commands
7

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

Comments

2

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

2 Comments

I found a few processes associated with Postgres but how do I kill them? I tried launchctl kill 5660com.postgresapp.Postgres2MenuHelper.1184 but am not getting the syntax right :/
You have to use the signal name or number also... so launchctl kill 9 ... If the process spawns again you can also try launchctl disable ... and launchctl unload. In Terminal try doing man launchctl for help.
2

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.

Comments

0

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.

Comments

0

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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.