Stop postgreSQL service on Mac via terminal
Asked Answered
S

9

41

Running postgreSQL 9.4.5_2 currently

I have tried

pg_ctl stop -W -t 1 -s -D /usr/local/var/postgres -m f

Normally no news is good news but after I will run

pg_ctl status -D /usr/local/var/postgres 

and get pg_ctl: server is running (PID: 536)

I have also tried

pg_ctl restart -w -D /usr/local/var/postgres -c -m i

Response message is:

waiting for server to shut down.......................... failed
pg_ctl: server does not shut down

I've also checked my /Library/LaunchDaemons/ to see why the service is starting at login but no luck so far. Anyone have any ideas on where I should check next? Force quit in the activity monitor also isn't helping me any.

Suppurate answered 9/12, 2015 at 7:54 Comment(0)
M
65

Sadly none of the previous answers help me, it worked for me with:

brew services stop postgresql

Cheers

Mullane answered 19/3, 2022 at 2:6 Comment(1)
may need to specify version: brew services stop postgresql@15Trematode
L
27

Restart it with

sudo -u postgres pg_ctl -D /Library/PostgreSQL/11/data stop
Loggerhead answered 28/11, 2018 at 19:49 Comment(3)
This is great, but one note - your command assumes you are in the same directory as the pg_ctl binary. Usually whatever install process you went through to get Postgres onto your machine puts that binary on a directory in your path. So what probably works more universally is sudo -u postgres pg_ctl -D /Library/PostgreSQL/11/data stopEuryale
Good solution. But not ./pg_ctl. Please use pg_ctl.Prynne
It doesn't work if your path is on a user directory (e.g. in ~/ because of permission issues. Run cd / to avoid file permissions issues when you switch the postgres user, which doesn't have access to your user home directory.Cacie
S
21

Tried sudo and su but no such luck. Just found this gui

https://github.com/MaccaTech/postgresql-mac-preferences

If anyone can help with the terminal commands that would be very much appreciated, but till then the gui will get the job done.

Suppurate answered 9/12, 2015 at 8:3 Comment(0)
T
14

Had the same issue, I had installed postgres locally and wanted to wrap in a docker container instead.

I solved it pretty radically by 1) uninstalling postgres 2) kill the leftover process on postgres port. If you don't un-install the process restarts and grabs the port again - look at your Brewfile form brew bundle dump to check for a restart_service: true flag.

I reasoned that, as I am using containers, I should not need the local one anyway, but !! attention this will remove postgres from your system.

brew uninstall postgres
...
lsof -i :5432 # this to find the PID for the process
kill - 9 <the PID you found at previous command> 

Note: if you still want to used psql you can brew install libpq, and add psql to your PATH (the command output shows you what to add to your .zshrc, or similar)

Temptress answered 27/7, 2021 at 18:10 Comment(0)
S
8

you can stop the server using this command

{pg_ctl -D /usr/local/var/postgres stop -s -m fast}
Spiral answered 15/9, 2017 at 7:49 Comment(1)
pg_ctl is not on my path. Where is it normally installed?Rhamnaceous
A
7

I just installed Postgres 15 on OS X Ventura and also could not figure out how to stop it. The following worked:

sudo -u postgres /Library/PostgreSQL/15/bin/pg_ctl -D /Library/PostgreSQL/15/data stop
Ado answered 12/5, 2023 at 2:52 Comment(0)
E
2

Another possible scenario is when you installed PostgreSQL via UI, the command to find the process is:

netstat -vanp tcp | grep 5432

Then you kill the PID

kill -9 PID_FROM_netstat
Ellary answered 9/12, 2015 at 7:54 Comment(1)
Almost any managed process is going to auto respawn if you just manually kill the processRossetti
M
2

Adding onto the solutions already stated :

if you decide to use the pg_ctl command, ensure that you are executing the command as a user with the permissions to access the databases/database server.

this means :

  • the current logged in user on your terminal should have those permissions

or

  • first run :
$ sudo su <name_of_database_user>

pg_ctl -D /Library/PostgreSQL/<version_here>/data/ stop

the same goes for the start command.

credit : https://gist.github.com/kingbin/9435292 (essentially hosted a file with the commands on github, saved me some time :^) )

Marron answered 9/1, 2023 at 2:21 Comment(0)
V
2

I had a stray docker container running Postgres that I had forgotten about.

Vacuum answered 28/1, 2023 at 16:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.