108

Having a bit of an issue with PostgreSQL on Mac OS X 10.8.4. I accidentally did brew rm postgresql --force while the postgres server was running. When I installed using brew install postgresql and ran pg_ctl -D /usr/local/var/postgres start I get:

pg_ctl: another server might be running; trying to start server anyway
server starting
FATAL:  lock file "postmaster.pid" already exists                                                                       
HINT:  Is another postmaster (PID 752) running in data directory "/usr/local/var/postgres"?

when I run "ps -ef | grep postgres" I get:

501   752   235   0 12:42PM ??         0:00.01 /usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgres -r /usr/local/var/postgres/server.log
501   754   752   0 12:42PM ??         0:00.00 postgres: checkpointer process       
501   755   752   0 12:42PM ??         0:00.34 postgres: writer process       
501   756   752   0 12:42PM ??         0:00.02 postgres: wal writer process       
501   757   752   0 12:42PM ??         0:00.01 postgres: autovacuum launcher process       
501   758   752   0 12:42PM ??         0:00.01 postgres: stats collector process       
501   945   452   0 12:58PM ttys000    0:00.00 grep postgres

when I try to stop the server with pg_ctl stop -D /usr/local/var/postgres -m fast or pg_ctl -D /usr/local/var/postgres stop -mi I get:

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

EDIT

which pg_ctl
/usr/local/bin/pg_ctl
pg_ctl --version
pg_ctl (PostgreSQL) 9.2.4

Gist of server.log: https://gist.github.com/anonymous/6106182

Sergey Telshevsky
  • 12,077
  • 6
  • 55
  • 78
Parker Hutchinson
  • 1,711
  • 2
  • 12
  • 13

8 Answers8

186

Had same issue, pg_ctl: server does not shut down. Furthermore, ps auxwww | grep postgres showed no postgres running, while pg_ctl -D /usr/local/var/postgres status showed that postgres is running. Restarting my mac didn't help, i even went as far as to reset SMC, but this SO Q/A gave me an idea what to look for next: brew issues.

Following this line of thought, i found this helpful blog post, that came down to following commands that solved this issue for me:

$ brew services list
$ brew services restart postgresql

Hope this will help someone.

Update - Unknown command: services

After a bit of investigation following one of the comments ('Unknown command: services'), i found out that the authors of Homebrew decided to remove services from the repo, seeing as no one wants to maintain this code.

You can read more about it here and here (related github tickets).

After some more digging, I found this repo that adds services on macs.

This is how I 'returned' services:

~ » brew tap gapple/services
~ » brew services
usage: [sudo] brew services [--help] <command> [<formula>]

Small wrapper around `launchctl` for supported formulae, commands available:
   cleanup Get rid of stale services and unused plists
   list    List all services managed by `brew services`
   restart Gracefully restart selected service
   start   Start selected service
   stop    Stop selected service

Options, sudo and paths:

  sudo   When run as root, operates on /Library/LaunchDaemons (run at boot!)
  Run at boot:  /Library/LaunchDaemons
  Run at login: /Users/user/Library/LaunchAgents

Here is another suggested solution: https://apple.stackexchange.com/questions/150300/need-help-using-homebrew-services-command. Didn't check it myself, so don't know if and how it works.

Community
  • 1
  • 1
Neara
  • 3,693
  • 7
  • 29
  • 40
  • 4
    This obsoletes so many bash aliases I've accumulated over the years for doing the exact same thing. Good look! – Alex Kestner Aug 06 '14 at 21:15
  • 2
    @naturalc updated my answer in response to your comment. tell me if it works ok for you. – Neara Jan 04 '15 at 14:27
  • 1
    Solved the problem for me. Thanks! – TotoroTotoro Nov 06 '16 at 03:38
  • For any Rails users out there, I was trying stop Postgres via usual Rake command, `bundle exec rake pg:stop`. But that wasn't working, leading to `pg_ctl: server does not shut down`. The workaround `brew services restart postgresql` worked for me. – zbeat Oct 06 '17 at 17:06
  • Thanks, man! I couldn't figure that it was actually a homebrew service running. Nice one! – Adrian Grzywaczewski Mar 05 '18 at 23:37
73

$ brew services list

$ brew services stop postgresql

Stopping postgresql, but it might take a while → Successfully stopped postgresql (label: homebrew.mxcl.postgresql)

  • 2
    Tried stopping with `pg_ctl ... stop` with no avail. Once I ran brew services stop postgresql, my pg_ctl...stop works as expected again now. Not sure how, but I'm glad this solved it! – Iggy Feb 23 '19 at 17:34
  • 2
    I needed to add the version with it. `brew services stop postgresql@10` – Ivor Scott Sep 01 '20 at 20:49
41

I was having the same problem...removing the launch agent solved the problem for me:

launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
rm ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Max Bentata
  • 435
  • 4
  • 6
  • 2
    This is spot on. the standard homebrew stop service command refused to work. – Kyias Oct 04 '22 at 10:31
  • 2
    This worked for me, while `brew services stop postgresql` and `brew services stop postgresql@10` didn't work for me. – Jasper Kuperus Oct 22 '22 at 08:19
  • Same, using brew kept failing and using DBngin did not help. But removing the launch agent worked ! Thanks – Arro Feb 06 '23 at 10:45
16

I had a similar issue. I had forgot that I integrated 'lunchy' a few days ago and was using it as a launchctl wrapper to initiate plist ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist on start up. pg_ctl was not effective because of the following line of code <key>KeepAlive<key>:

      <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
     3 <plist version="1.0">
     4 <dict>
     5   <key>KeepAlive</key>
     6   <true/>
     7   <key>Label</key>
     8   <string>homebrew.mxcl.postgresql</string>
     9   <key>ProgramArguments</key>
    10   <array>
    11     <string>/usr/local/opt/postgresql/bin/postgres</string>
    12     <string>-D</string>
    13     <string>/usr/local/var/postgres</string>
    14     <string>-r</string>
    15     <string>/usr/local/var/postgres/server.log</string>
    16   </array>
    17   <key>RunAtLoad</key>
    18   <true/>
    19   <key>WorkingDirectory</key>
    20   <string>/usr/local</string>
    21   <key>StandardErrorPath</key>
    22   <string>/usr/local/var/postgres/server.log</string>
    23 </dict>
    24 </plist>

Trying to kill the process directly didn't work because I needed to unload the plist.

    launchctl unload homebrew.mxcl.postgresql.plist
user3155603
  • 161
  • 1
  • 2
  • Note the .plist file might not have the exact same name. In my case it was `homebrew.mxcl.postgresql92.plist`. Check yoself. – MCP Oct 30 '14 at 17:01
12

I got round this error by using the command

pg_ctl stop -m immediate

pg_ctl start

I did not need to unload the plist this way.

eebbesen
  • 5,070
  • 8
  • 48
  • 70
  • 6
    I get pg_ctl: no database directory specified and environment variable PGDATA unset – SuperUberDuper Feb 29 '16 at 22:20
  • 1
    getting the same error: no database directory specified and environment variable PGDATA unset – jjj Nov 02 '17 at 13:03
  • 2
    @SuperUberDuper You should add -D and specify the db location. For example: pg_ctl stop -m immediate -D . "." stands for the current directory, assuming you are on the database directory – Richard Miller Dec 13 '18 at 23:50
9

turns out that my brew.plist for postgres needed to be unloaded and removed at ~/Library/LaunchAgents/ . After a restart all is well... closing question.

Parker Hutchinson
  • 1,711
  • 2
  • 12
  • 13
9

When relying on brew-installed PostgreSQL, the following command worked for shutting it down.

> brew services stop postgresql

stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)

As mentioned in another answer grepping through the list of processes and then killing anything postgres-related does not work.

ps aux | grep postgres
kill $PID
Peter Lustig
  • 941
  • 11
  • 23
0

I didn't have postgres started via brew services, so couldn't stop it that way.

This worked.

$ ps -ef | grep postgres
501   547     1   0  2:07pm ??         0:00.29 /Applications/Postgres.app/Contents/Versions/9.6/bin/postgres -D /Users/username/Library/Application Support/Postgres/var-9.6 -p 5432

Use -D paramater from above.

$ pg_ctl stop -D "/Users/username/Library/Application Support/Postgres/var-9.6"
M3RS
  • 6,720
  • 6
  • 37
  • 47