"FATAL: lock file "postmaster.pid" already exists"

74

36

I just reinstalled postgres via brew install postgres

I ran initdb /usr/local/var/postgres -E utf8 but got this:

The files belonging to this database system will be owned by user "atal421".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default text search configuration will be set to "english".

initdb: directory "/usr/local/var/postgres" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/var/postgres" or run initdb
with an argument other than "/usr/local/var/postgres".

so, I rm -rf the postgres folder and ran it again:

 initdb /usr/local/var/postgres -E utf8

it said everything was okay:

Success. You can now start the database server using:

    postgres -D /usr/local/var/postgres

so, I ran that command and got:

postgres -D /usr/local/var/postgres


FATAL:  lock file "postmaster.pid" already exists
HINT:  Is another postmaster (PID 13731) running in data directory "/usr/local/var/postgres"?

Now when I look at my Activity Monitor I can see 6 instances of postgress.

How do I fix this?

AdamT

Posted 2013-02-16T16:31:06.023

Reputation: 893

You probably see one instance of postgres with a postmaster and five utility backends. PostgreSQL is a multi-process architecture. – Craig Ringer – 2013-02-17T23:39:36.837

Answers

113

Public service announcement: never delete postmaster.pid. Really. Great way to get data corruption.

You already had PostgreSQL installed, and you deleted the data dir without stopping the running server. So you now have some orphan PostgreSQL server processes that are managing data files that've been deleted, so they're no longer accessible in the file system and will be fully deleted when the last open file handle to them is closed. You can't use pg_ctl to shut the server down like normal because you've deleted the cluster datadir, so you must simply kill the processes. Kill the postmaster (do not use kill -9, just an ordinary kill will do) and the rest will shut down too.

You will then be able to start a new server in the datadir against the freshly initdb'd data.

It is highly likely that you will experience conflicts down the track unless you uninstall the other older version of PostgreSQL.

In a nutshell:

cat /usr/local/var/postgres/postmaster.pid

Note down the number on the first line, which is the pid of the postmaster.

Verify with ps that the pid is that of a postgres postmaster.

Kill the postmaster process with the following command, replacing 'PID' with the number you have noted down. Again, do not use kill -9 or kill -KILL, just use a plain kill, i.e. a SIGTERM:

kill PID

If the pid is not that of a postgres postmaster, manually kill any postgres backends that may still be running, verify that they are no longer running, and only then remove postmaster.pid. (You must also verify that the postmaster.pid is not on shared storage where the server could be running on some other VM/host).

Craig Ringer

Posted 2013-02-16T16:31:06.023

Reputation: 2 630

yeah. that was spot on. – Amos Folarin – 2015-06-02T15:52:55.473

7It should be mentioned that after a hard crash the PID file might survive, while the process dies. In that case the PID in the PID file can point to a process that's got nothing to do with Postgres. See second answer for that case. – febeling – 2016-05-31T11:46:03.560

Thank you @febeling I am pretty upset that this answer does not address this fact. I literally just killed a process that had nothing to do with postgres by running the kill command on the pid from that postgres.pid – Colin D – 2017-03-20T15:17:39.727

@ColinD Answer updated. – Craig Ringer – 2017-03-21T02:29:10.617

2A plain kill PID didn't work for me. I needed kill -3 PID. In my case I had done a shutdown which may have killed terminal windows without stopping the processes properly. The kill -3 PID killed the process and its children successfully letting me start postgres again. – Paul Masri-Stone – 2017-06-26T08:31:43.113

kill -9 PID only worked for me – VAD – 2018-08-14T10:52:03.390

you literally made my day – Yasser Sinjab – 2019-01-09T18:35:05.457

couldn't find the process with the PID in postmaster.pid. did kill <PID> anyway, and it worked #facepalm – Ankan-Zerob – 2019-06-19T16:44:00.710

this worked for me! – Tone – 2013-11-26T06:53:41.133

This works. Had a problem where I emptied my trash and it seems some data files probably were in there...not sure how, but they were. Once I killed the old process it worked fine. – Dan L – 2014-06-04T15:30:59.790

48

Another possibility is that you had a hard shutdown and the postgres process died without cleaning up its pid file. This happens to me when my laptop's battery dies.

This solution is not for a production system, and you should really make sure the postgres daemon is not running, but I use my laptop for coding and I'm not worried about needing to regenerate my databases.

So if another process -- or none at all -- is running on that port, just delete the pid file, e.g.

rm /usr/local/var/postgres/postmaster.pid

and postgres will soon start up fine.

To find out if another process is running on that port, you can do

ps wax | grep `head -1 /usr/local/var/postgres/postmaster.pid`

Then run

tail -f /usr/local/var/postgres/server.log 

to see if it worked. You should see

FATAL:  lock file "postmaster.pid" already exists
HINT:  Is another postmaster (PID 933) running in data directory "/usr/local/var/postgres"?
FATAL:  lock file "postmaster.pid" already exists
HINT:  Is another postmaster (PID 933) running in data directory "/usr/local/var/postgres"?
LOG:  database system was interrupted; last known up at 2014-05-25 09:41:32 PDT
LOG:  database system was not properly shut down; automatic recovery in progress

(or at least that's what I just saw after I did the above :-) )

(And really, shouldn't Postgres be smart enough to realize that there is no process with PID 933 and remove the bogus pid file on its own?)

AlexChaffee

Posted 2013-02-16T16:31:06.023

Reputation: 581

1This was the case for me. I had another process coincidentally running on the same PID as the postmaster.pid file was pointing to. This was several days after an unclean shutdown (laptop installation of Postgres on OSX via homebrew). – Jesse Buchanan – 2014-10-07T03:04:53.450

1My mac hung on the login screen and I had to power it down. That ended up leaving a postmaster.pid file around that referenced a PID which got used for something else on the next reboot and postgres wouldn't come up. I tried killing the PID (like the post by craig-ringer recommended), but that didn't help. However, rm postmaster.pid worked for me. I don't see any data corruption (but in any case, this is just a development machine). – Steven Chanin – 2016-01-25T16:43:30.637

Same thing for me. I had shut down my mac without stopping the local Rails server. – Bruno Paulino – 2016-06-20T19:47:16.727

1I keep coming back to this thread whenever this happens - because I can never remember where the pid file is, so I always have to look it up :P – haslo – 2017-04-20T12:55:35.507

This happened to me with the Postgres.app. After deleting ~/Library/Application Support/Postgres/data/postmaster.pid I was up and running again. – Rob Johansen – 2017-09-23T02:21:00.210

Those backslashes aren't actually supposed to be before the backticks in the "ps" call, right? – M. Justin – 2017-12-01T16:15:29.643

@M.Justin Right. Maybe StackOverflow changed their escaping code after I wrote that. Fixed. – AlexChaffee – 2017-12-11T16:05:46.247

8

I tried all of this to no avail after upgrading to Yosemite broke my postgres (installed via homebrew).

Then I stumbled on this blog post: http://ruckus.tumblr.com/post/100355276496/yosemite-upgrade-breaks-homebrew-installed-postgres

First I needed to create the missing directories that were apparently wiped out during the upgrade (thanks Apple!).

$ cd /usr/local/var/postgres

$ mkdir {pg_tblspc,pg_twophase,pg_stat_tmp}

Then just start postgres again using the normal homebrew launch sequence:

$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

Thanks Ruckus Notes for helping solve my problem. Hopefully it helps you as well.

Billy G

Posted 2013-02-16T16:31:06.023

Reputation: 81

4

HARD REBOOT INSTRUCTIONS

I had this same issue after a hard reboot. After checking the postmaster.pid file's pid, I noticed I had no process running. I didn't want to hard delete the .pid file, instead I used a pg-stop alias I had created in my .bash_profile. this alias just runs

pg_ctl -D /usr/local/var/postgres stop -s -m fast

For Reference

# psql
alias pg-start='pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start'
alias pg-stop='pg_ctl -D /usr/local/var/postgres stop -s -m fast'

log output after pg-stop

LOG:  database system was interrupted; last known up at 2016-04-25 10:51:08 PDT
LOG:  database system was not properly shut down; automatic recovery in progress
LOG:  record with zero length at 0/274FA10
LOG:  redo is not required
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started
LOG:  received smart shutdown request
LOG:  autovacuum launcher shutting down
LOG:  shutting down
LOG:  database system is shut down
LOG:  database system was shut down at 2016-04-25 13:11:04 PDT

brew

I thought I should also mention here that if you have installed postgres with homebrew you should give brew services a look. That is now how I prefer to start/stop my databases.

XXXXX:~ chris$ brew services list
Name       Status  User  Plist
mongodb    stopped
postgresql started chris /Users/chris/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
redis      started chris /Users/chris/Library/LaunchAgents/homebrew.mxcl.redis.plist

Chris Miller

Posted 2013-02-16T16:31:06.023

Reputation: 41

The brew info was just what I needed, thanks! – dnatoli – 2016-11-03T23:14:03.297

1

Sometimes the humble pg_ctl -w restart can do the trick :-)

sekmo

Posted 2013-02-16T16:31:06.023

Reputation: 111

Love when the top answer is YOU ARE DOOMED! TOUCH NOTHING! and then one little command gets me running. (In my case the pid in /usr/pgsql/9.3/data/postmaster.pid was not present in ps aux.) – Noumenon – 2018-05-08T14:50:04.757

1

I received this error after, I think, my computer crashed. PostgreSQL couldn't even start because of this error so killing the process wasn't the solution. I simply backed-up and then deleted the postmaster.pid file and then the error stopped and PG was able to start again.

Jeff

Posted 2013-02-16T16:31:06.023

Reputation: 394

0

Deleting postmaster.pid is actually a really decent thing to do at every boot, blindly. That's what my system does. Because you've just booted up, you know there is no Postgres process running, and if you're recovering from an unclean shutdown, this file will be there preventing your recovery.

A better design for Postgres would be to put the postmaster.pid file in the /run filesystem, so it is guaranteed to get deleted at every reboot. Many other servers work that way.

giraffedata

Posted 2013-02-16T16:31:06.023

Reputation: 1