73

I have recently installed PostGIS on my Mac (El Capitan 10.11.4, Postgres is version 9.5.1) using Homebrew, and I am following these instructions - http://morphocode.com/how-to-install-postgis-on-mac-os-x/

When I try to start Postgres using

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

I get the following error:

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

So I spent a few hours researching how to address this, but to no avail.

Notably, I tried to kill the PID as recommended in an answer on Superuser - https://superuser.com/questions/553045/fatal-lock-file-postmaster-pid-already-exists- (in the case above, I ran kill 208), but as soon as I tried to start Postgres again, I got the same error, albeit with a different PID number. I saw a few people recommended deleting the postmaster.pid file, but I feel like maybe I should save that as a last resort...

Admittedly part of the reason I'm not sure how to fix this is that I'm not really clear on what the postmaster even is - I'm just starting to learn about all of this.

Hopping into a Postgres database via the psql db_name command works just fine, for what it's worth.

skwidbreth
  • 7,888
  • 11
  • 58
  • 105

13 Answers13

68

Posting this in case it helps someone else:

I was having this same problem as the OP after a hard reboot when my laptop crashed. What helped me was running the following command to see what PID was associated with postmaster.pid:

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

The first number that appears will be the PID. Looking in Activity Monitor, I was able to see that Postgres was running, but without a PID number that matched the one shown.

Instead of the steps outlined in the answer referenced on Superuser, I restarted my laptop properly and then opened up Terminal and ran

brew services restart postgresql

This worked without having to remove postmaster.pid, which I saw a few other posts recommend. Sometimes it's the simple solutions that work.

Kamil Gosciminski
  • 16,547
  • 8
  • 49
  • 72
aronmoshe_m
  • 908
  • 1
  • 6
  • 15
  • 4
    Running `brew services restart postgresql` didn't work alone, but coupled with a full laptop restart it resolved my issue. Thanks! – Iron John Bonney Sep 23 '20 at 18:12
  • 7
    Another option if postgres is an an inconsitent state (maybe from an abrupt reboot) is simply to remove the postmaster.pid file, and then attempt to restart postgres: `rm /usr/local/var/postgres/postmaster.pid` – Hartley Brody May 07 '21 at 19:23
  • 4
    On M1 macs, the command is `rm /opt/homebrew/var/postgresql/postmaster.pid` – Matthew Feb 23 '22 at 05:30
  • 1
    kill that process will spawn another one automatically, I have to reboot – ychz Sep 26 '22 at 17:50
62

I add here what worked for me, after a long time of searching:

  1. Delete the postmaster.pid file:

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

  2. Restart your postgres:

    brew services restart postgresql

Hope this helps someone ...

Update 8/2022:

As Mike commented, for M1 Mac you would replace stage 1 with:

rm /opt/homebrew/var/postgresql/postmaster.pid

With M1 and specify Postgres Version @ 14

rm -rf /opt/homebrew/var/postgresql@14/postmaster.pid
Taimoor Changaiz
  • 10,250
  • 4
  • 49
  • 53
Baruch Gans
  • 1,415
  • 1
  • 10
  • 21
  • 4
    On my M1 Mac I uses homebrew to install PostgreSQL and the directory for postmaster.pid is /opt/homebrew/var/postgres/postmaster.pid – Mike Rayco Aug 25 '22 at 02:19
  • Everything that is from homebrew is now on `/opt/homebrew/` , even `/opt` from the system, which makes this bit of odd path `/opt/homebrew/opt`, but most tutorials will mention `/usr/local/`. So just make sure you change everything from `/usr/local/…` to `/opt/homebrew/…`. Btw, this answer also worked for me with this new folder placement. – lucasarruda Mar 07 '23 at 17:55
32

It often happens to me in OSx, when my system shutdown unexpectedly.

You can just remove the file postmaster.pid.

cd Library/Application Support/Postgres/var-{postgres-version}

and remove the postmaster.pid file

in case you use brew then your path should be something like:

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

restart the Postgres by using this command

pg_ctl -D /usr/local/var/postgres restart
svelandiag
  • 4,231
  • 1
  • 36
  • 72
Sobin Sunny
  • 1,121
  • 10
  • 14
  • Thanks. cd Library/Application Support/Postgres/var-{postgres-version} had the right path for postmaster.pid – iCyborg Sep 09 '20 at 16:50
  • bump for "when my system shutdown unexpectedly". Confirmed very reproduceable over the last several months. Thank you for this answer – jseashell Apr 06 '22 at 20:48
  • On my M1 Mac I uses homebrew to install PostgreSQL and the directory for postmaster.pid is /opt/homebrew/var/postgres/postmaster.pid – Mike Rayco Aug 25 '22 at 02:19
23

Since you can connect to the database, you don't need to start the server again - it's already running.


pg_ctl is used to control the PostgreSQL server. Since your server is already started, your command:

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

Returns an error, saying that there is a lock on postmaster.pid - which is true since there is already a server running under that PID.


There are two ways:

  1. The most basic way - skip that step, your server is already running!
  2. Executing a needless operation - stopping the server, and then starting it again.

You could stop your server doing :

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

So that you won't have the lock on postmaster anymore and you could use your command to start it again.

John R Perry
  • 3,916
  • 2
  • 38
  • 62
Kamil Gosciminski
  • 16,547
  • 8
  • 49
  • 72
11

Postmaster is the main PostgreSQL process. You're trying to start PostgreSQL that's already running (and you're saying yourself you can connect to it). Just skip that step of your process.

Jakub Kania
  • 15,665
  • 2
  • 37
  • 47
9

When the system shutdown unexpectedly, my postgres crashs and i'm unable to connect to it.

What worked for me was:

1˚ Check postgres log:

tail -n 10000 /usr/local/var/log/postgres.log

2˚ Find the PID of postgress, should look like this:

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

3˚ Kill that process:

kill 707

4˚ Restart your postgres

brew services restart postgresql

After those steps i was able to connect to the database within my rails application.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Clayton Oliveira
  • 625
  • 1
  • 6
  • 15
  • 1
    Before killing that process, check to make sure what it is! The postmaster process may have already stopped (e.g. after a computer restart), so make sure you're not killing a different process. – Hosam Aly Nov 29 '21 at 14:12
  • Easy to check with `ps aux | fgrep postgresql` – Nick Jan 12 '23 at 18:20
4

If you got no important data to lose :

sudo killAll postgres
brew services restart postgresql

AGAIN : You could get data corrupted by doing this !

do it at your own risk !

Diego Favero
  • 1,969
  • 2
  • 22
  • 32
4

I am using mac and these step work for me:-

step1: cd Library/Application\ Support/Postgres (most commonly your Postgres installation will be located here)

step2: cd var-13 (if you are using version 12 then use cd var-12. Hope got the point)

step3: ls (As you can see among the files you find the postmaster.pid, perfect.)

step4: rm postmaster.pid

When you have removed the stale postmaster.pid file you can restart PostgreSQL and everything should work as normal.

HEMANTA
  • 71
  • 7
1

My OSX laptop had shutdown unexpectedly, and I was getting a stale postmaster.pid error in the PostgresApp. Shutting down my laptop and turning it back on again solved the problem.

remnantkevin
  • 11
  • 1
  • 1
  • I spent 10 minutes trying to hunt down and remove this `postmaster.pid` file... and then rebooted my Mac in 30 seconds to solve the issue. – Matt May 19 '20 at 12:06
1

After running the following commands

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

brew services restart postgresql

The error lock file "postmaster.pid" already exists comes up again.


When we run launchctl list | grep postgres

28618   0   homebrew.mxcl.postgresql

The existing file "postmaster.pid" was created by this daemon process hosted by launchctl.

We try to stop the homebrew.mxcl.postgresql through

sudo launchctl stop homebrew.mxcl.postgresql
launchctl disable homebrew.mxcl.postgresql

Unfortunately, none of them could stop the homebrew.mxcl.postgresql.

The reason is Disable and enable an agent using (persists between boots)

https://apple.stackexchange.com/questions/105892/disable-services-in-osx-services-msc

launchctl enable <name> or launchctl disable <name>


Two ways to solve it when the error lock file "postmaster.pid" already exists comes up again

  • In order to stop an agent immediately through
launchctl kill homebrew.mxcl.postgresql
  • Restart your desktop and run brew services start postgresql@14. Now, PostgreSQL could start successfully.

Hope it could help someone who met the same issue again.

zangw
  • 43,869
  • 19
  • 177
  • 214
0

This worked for me. First locate postmaster.pid (for me it was in the var directory as seen below, although it will be different on depending on your operating system). Then get rid of postmaster.pid, then kill the postgres process, then start/restart postgres service.

cd /var/lib/pgsql/data/

rm postmaster.pid

sudo pkill -u postgres

sudo systemctl start postgresql.service

user3062459
  • 1,587
  • 7
  • 27
  • 39
0

I tried everything listed here but none of them worked (M1 Mac, postgres@14).

I had to nuke the Postgres directory at /opt/homebrew/var/postgres and reinstall with brew.

john
  • 1,561
  • 3
  • 20
  • 44
-1

If you have installed postgres with brew then simply run the following command and it will manage everything

 brew services restart postgresql
Abdul
  • 14
  • 1