9

I am trying to start the server and getting an error

Port 5432 is already in use

I have brew uninstall postgress

which postgres

gives me nothing.

Activity monitor has 14 postgres processes which I cannot kill. Force quit kill the process and restarts it with another pid. The same with sudo kill -9 PID it kills the process and restarts it with another PID.

clemens
  • 16,716
  • 11
  • 50
  • 65
Petran
  • 7,677
  • 22
  • 65
  • 104

7 Answers7

28

If you are running into this problem on OSX, do the following:

  1. Find out what is running on that port:
    $ lsof -n -i4TCP:5432

    COMMAND     PID         USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
    python2.7 28687 afdasdfasd    3u  IPv4 0x2f18e1284963d3e3      0t0  TCP 127.0.0.1:54970->127.0.0.1:postgresql (CLOSE_WAIT)
  1. Kill it
    $ kill -9 28687
  1. Restart postgresapp
olegtaranenko
  • 3,722
  • 3
  • 26
  • 33
josephmisiti
  • 9,862
  • 11
  • 56
  • 72
  • The command `lsof -n -i4TCP:5432` did the trick for me, "netstat" didn't yield anything for port 5432 on OSX. Thanks! – leo Jul 24 '19 at 05:56
  • This one worked for me as well. In my case, I found out I was using DataGrip, and it seems it kept the port in use even when PostgreSQL was not running anymore. – Oscar Calderon Feb 05 '20 at 21:29
22

Have you checked for a launch daemon? It controls the Postgres process when Postgres is installed with Homebrew, and it automatically restarts Postgres after it is killed. Try

sudo launchctl list

or

sudo launchctl list | fgrep postg

to find the name of the daemon. You can stop the daemon with sudo launchctl stop <name> where name depends on the result of the first command.

clemens
  • 16,716
  • 11
  • 50
  • 65
17

Askubuntu provided an answer that worked:

sudo pkill -u postgres

Source: Nicely stop all postgres processes

tread
  • 10,133
  • 17
  • 95
  • 170
  • The OP runs Postgres under MacOS (the _Activity Manager_ is a very subtle hint for that) from the Homebrew package. Homebrew uses `launchctl` to start and stop Postgres. If you stop Postgres with `kill`, it will be restarted by `launchctl`. The OP has already tried your solution without luck. – clemens Aug 05 '18 at 17:23
  • 1
    Alright, but I came to this question because I needed an answer and there is no explicit mention of homebrew. So for others that come to this question, my solution may be a better fit. – tread Aug 05 '18 at 21:31
10

$ brew services stop postgresql

This will kill all processes and let you start the server.

Roger Camps
  • 129
  • 1
  • 7
3

You can get the list of ports using:

sudo launchctl list

Then enter the application name, and using this command to get the port nunber:

sudo launchctl list | fgrep postg

In my case, the port is 83. Now use:

kill 83

then

sudo kill 5432
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
0

Run those commands

  • To check what is running on port 5432 - $ sudo lsof -i :5432
  • To kill Postgres - $ sudo pkill -u postgres
Suraj Malgave
  • 133
  • 1
  • 4
-1

Use this:

brew services stop postgresql

Good luck!

Mana Sh
  • 1
  • 1
  • This solution is already provided in [this existing answer](https://stackoverflow.com/a/52958226/5320906). When answering old questions please ensure that your answer provides a distinct and valuable contribution to the Q&A. – snakecharmerb Jul 17 '22 at 09:22