37

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

flaurida
  • 411
  • 1
  • 5
  • 9
  • 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. – Laurenz Albe Jun 17 '17 at 18:57
  • 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`) –  Jun 17 '17 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 – Abdel P. Sep 27 '18 at 14:29

8 Answers8

46

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

brew services stop postgres
BHendricks
  • 4,423
  • 6
  • 32
  • 59
Moebius
  • 6,242
  • 7
  • 42
  • 54
12

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

Community
  • 1
  • 1
Stanislav Kr.
  • 504
  • 4
  • 12
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
Steven Shi
  • 1,022
  • 1
  • 10
  • 10
7

Try to run this command:

sudo pkill -u postgres
tn2000
  • 642
  • 1
  • 8
  • 13
3

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
epurdy
  • 151
  • 1
  • 12
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
l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • 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 :/ – flaurida Jun 17 '17 at 17:46
  • 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. – l'L'l Jun 17 '17 at 17:58
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.

Daniel
  • 2,028
  • 20
  • 18
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 :)