How to stop postgres server on MacOS

This post describes how I was able to stop pg 14 server on macos that I installed from EDB website.

The commonly described solution is to use brew but that did not work for me. E.g., when I ran following command:

$ brew services list

all I got was:

Name    Status User File
unbound none

I could verify that pg was running using:

☺  netstat -vanp tcp | grep 5432

which gave me following output:

tcp6       0      0  ::1.5432               ::1.50268              ESTABLISHED  399652  146808  36930      0 00102 0000000c 0000000000176e9a 00000080 01000800      1      0 000001
tcp6       0      0  ::1.50268              ::1.5432               ESTABLISHED  405849  146808  36857      0 00102 00000008 0000000000176e99 00000081 00000800      1      0 000001
tcp6       0      0  ::1.5432               ::1.50261              ESTABLISHED  399637  146808  36925      0 00102 0000000c 0000000000176e6f 00000080 01000800      1      0 000001
tcp6       0      0  ::1.50261              ::1.5432               ESTABLISHED  405839  146808  36857      0 00102 00000008 0000000000176e6e 00000081 00000800      1      0 000001
tcp6       0      0  ::1.5432               ::1.50243              ESTABLISHED  399655  146808  36919      0 00102 0000000c 0000000000176e1a 00000080 01000800      1      0 000001
tcp6       0      0  ::1.50243              ::1.5432               ESTABLISHED  405851  146808  36857      0 00102 00000008 0000000000176e19 00000081 00000800      1      0 000001
tcp6       0      0  ::1.5432               ::1.50229              ESTABLISHED  292573  146808  36914      0 00102 0000000c 0000000000176d82 00000080 01000800      1      0 000001
tcp6       0      0  ::1.50229              ::1.5432               ESTABLISHED  282167  146808  36857      0 00102 00000008 0000000000176d81 00000081 00000800      1      0 000001
tcp4       0      0  *.5432                 *.*                    LISTEN       131072  131072    296      0 00000 00000006 00000000000002d4 00000000 00000900      1      0 000001
tcp6       0      0  *.5432                 *.*                    LISTEN       131072  131072    296      0 00000 00000006 00000000000002d3 00000000 00000800      1      0 000001

I tried stopping the server using:

☺  sudo pg_ctl stop -D /Library/PostgreSQL/14/data

but that gave:

pg_ctl: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.

Then I logged in as

sudo su -

but that also did not work. What worked was following:

☹  sudo su postgres

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
bash-3.2$ /Library/PostgreSQL/14/bin/pg_ctl stop -D /Library/PostgreSQL/14/data
waiting for server to shut down.... done
server stopped

after that when I ran:

 netstat -vanp tcp | grep 5432

it gave me:

tcp6       0      0  ::1.5432               ::1.50268              FIN_WAIT_2   399652  146808  36930      0 02131 0000000c 0000000000176e9a 00000080 01000800      0      0 000001
tcp6     117      0  ::1.50268              ::1.5432               CLOSE_WAIT   405849  146808  36857      0 00122 00000008 0000000000176e99 00000081 00000800      1      0 000001
tcp6       0      0  ::1.5432               ::1.50261              FIN_WAIT_2   399637  146808  36925      0 02131 0000000c 0000000000176e6f 00000080 01000800      0      0 000001
tcp6     117      0  ::1.50261              ::1.5432               CLOSE_WAIT   405839  146808  36857      0 00122 00000008 0000000000176e6e 00000081 00000800      1      0 000001
tcp6       0      0  ::1.5432               ::1.50243              FIN_WAIT_2   399655  146808  36919      0 02131 0000000c 0000000000176e1a 00000080 01000800      0      0 000001
tcp6     117      0  ::1.50243              ::1.5432               CLOSE_WAIT   405851  146808  36857      0 00122 00000008 0000000000176e19 00000081 00000800      1      0 000001

This shows the port is shutting down. After a while there is no output from the netstat command if you run it again. so this is how I was able to stop the server.

This entry was posted in Computers, programming, Software and tagged . Bookmark the permalink.

Leave a comment