108

I ve installed Postgresql and then ran a bunch of rails apps on my local Mac OSX Mountain Lion and created databases etc. Today after a while when I launched pgAdminIII and tried to launch a database server I got this error:

enter image description here

A quick google showed this post. More browsing pointed to the fact that there might be some sort of postmaster.pid file lying around that might be the root cause of this. If I delete that things would be fine.

However, before I go deleting stuff on my computer I wanted to make sure Im debugging this in a systematic way which would not result in more problems.

Somewhere I read that before deleting that file, I need to run this command:

  ps auxw | grep post

If I get no results then, its OK to delete the file. Else not. Well, I got this result of that command:

  AM               476   0.0  0.0  2423356    184 s000  R+    9:28pm   0:00.00 grep post

So now of course Im throughly confused.

So what should I do?

Here is part of my postgres server error log:

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

Postgresql is still not running, still get the same error and nothing has changed. Im too chicken to delete things without checking on SO.

Could some of you experts please guide a noob.

Thanks

Community
  • 1
  • 1
banditKing
  • 9,405
  • 28
  • 100
  • 157
  • First, I'm not an expert. With that said, it sounds like, your database wasn't shutdown properly. I would remove the pid file and try to start the database. – fbynite Jul 23 '13 at 02:16
  • Yeah thats what I want to do, but I read somewhere that this can cause permanent damage to the database if a pid is running while this is done. Not sure what that was all about – banditKing Jul 23 '13 at 02:22
  • @banditKing You should never need to delete `postmaster.pid`. The server deletes it on startup if it's invalid(stale), and it's important for data protection. Please **explain how you installed PostgreSQL on your computer** to start with - via EnterpriseDB installer, homebrew, Postgres.app, what? Also mention the PostgreSQL version. Finally, if you explicitly add "localhost" to the connection options in Pgadmin-III does it work? If so, you're being bitten by a really bad packaging decision Apple made a while ago... – Craig Ringer Jul 23 '13 at 02:33
  • @CraigRinger thanks for clarifying. I do not remember how I installed it, it was a while ago. Is there anything I can do? Or the data in there is just test data so, worst case scenario, I can uninstall and reinstall. Any good blogs/tutorials on how to do so correctly? – banditKing Jul 23 '13 at 02:38
  • 1
    @banditKing Tried rebooting? You can identify how you installed from this: http://wiki.postgresql.org/wiki/Installers/Mac_OS_X . It looks like you probably used Homebrew. If the PostgreSQL server is incorrectly failing to delete `postmaster.pid` then you *can* remove it after verifying that there are no `postgres` processes running, it's just *vital* that you be sure there are in fact no PostgreSQL processes. Failure of the server to remove a stale `postmaster.pid` seems like a bug to me, though. – Craig Ringer Jul 23 '13 at 02:52
  • @CraigRinger Thanks. I verify as shown above that there are no postgres process running but how can I delete one if I find it? Thanks – banditKing Jul 23 '13 at 03:38
  • @banditKing Delete one what? Do you mean terminate a `postgres` process? the `kill` command. But in your case, if a reboot doesn't sort this out, you're probably OK to remove the pid file. I'm disturbed that you need to though, the postmaster should delete it automatically. Is there some other process with pid 171 in existence? – Craig Ringer Jul 23 '13 at 03:42

4 Answers4

250

I had the same problem today on Mac Sierra. In Mac Sierra you can find postmaster.pid inside /Users/<user_name>/Library/Application Support/Postgres/var-9.6. Delete postmaster.pid and problem will be fixed.

r3b00t
  • 6,953
  • 6
  • 27
  • 37
  • 22
    Deleted `postmaster.pid` in this location and the problem was fixed. Thanks! – JLF Oct 10 '17 at 17:46
  • 1
    This is also where I needed to find the `postmaster.pid` file since I am using the Postgres.app – Jarsen Nov 10 '17 at 05:24
  • 4
    For Postgres.app it's the same: could be that it's located here `/Users//Library/Application\ Support/Postgres/var-10/postmaster.pid ` – Simon Franzen Aug 26 '18 at 15:13
  • 1
    I am also using the Postgres.app running v10. If my Mac has an unexpected shutdown while Postgres is running then I need to go and delete the postmaster.pid file when I restart the computer. Does anyone know if that is the expected behaviour of the app? Seems like a strange design if deliberate. – Alexander Oct 14 '18 at 16:15
  • Make sure the process is spun down before removing this file. You can run `cat postmaster.pid` in the directory to peek in and reference that you don't see the PID running in something like activity monitor. The Postgres UI will alert you about this, but I thought I should put it here before people get willy nilly and how to check yourself. It should also be sufficient to look for `postgres` in the activity monitor and kill anything that's related, or just restart your system and then delete the file. Not doing this could result in a corrupt db – CTS_AE Jan 18 '19 at 22:08
73

This can happen if the database did not shut down correctly. To fix it simply delete the postmaster.pid file. The location differs based on your OS:

MacOS:

 rm /Users/<user_name>/Library/Application\ Support/Postgres/var-9.6/postmaster.pid

or using Postgres.app:

 rm /Users/<user>/Library/Application\ Support/Postgres/var-10/postmaster.pid

Linux:

 rm /usr/local/var/postgres/postmaster.pid
sqren
  • 22,833
  • 7
  • 52
  • 36
  • 2
    Thanks, this solution works! After removing `postmaster.pid` and run `postgres -D /usr/local/var/postgres`, it notices me about the error cause: "database system was not properly shut down; automatic recovery in progress" – Hoang Le Jul 29 '15 at 02:31
  • this solved my issue – Fabrice Yopa Mar 19 '18 at 11:33
  • This works. In my case, the pid file didn't get removed because of a hard reboot, so just had to back it up. – Clucking Turtle Mar 19 '18 at 22:37
32

I have the database working now.

Here are the steps I took:

  1. I rebooted my computer
  2. I opened the terminal and ran cd /
  3. Then I did ls -la
  4. Ensured that I could get to MackintoshHD/usr/local/var/postgres
  5. Then did ls -la
  6. Here I saw the postmaster.pid file
  7. I ran this command cp postmaster.pid ~/Desktop which copied the file to my desktop.I like to do this if I am deleting files. If something does wrong i can put it back
  8. Then I ran this command to remove the file from the postgres directory rm -r postmaster.pid
  9. I went to my pgadmin3 gui and fired it up. and Voila it worked :)

Thanks to @Craig Ringer for his help

Timothée Boucher
  • 1,535
  • 12
  • 30
banditKing
  • 9,405
  • 28
  • 100
  • 157
  • 7
    Mine was not in /usr/local/var/postgres but in /Users/[myhome]/Library/Application Support/Postgres/var-9.5/ - last directory depends on your version, deleted postmaster.pid and worked like a charm, thank you – Radek Sep 01 '17 at 03:48
4

I'm using Postgres.app, and the below worked for me:

I entered the commands into my terminal below, locating the Postgres folder beforehand and not using "justin".

$declare -x PGDATA="/Users/justin/Library/Application Support/Postgres/var-9.4"

$pg_ctl restart -m immediate

As Justin explains in his post, the output after this was:

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

After entering the command again:

$pg_ctl restart -m immediate

It worked and I got this message:

pg_ctl: old server process (PID: 373) seems to be gone starting server anyway server starting LOG: database system was interrupted; last known up at 2015-07-28 18:15:26 PDT LOG: database system was not properly shut down; automatic recovery in progress LOG: record with zero length at 0/4F0F7A8 LOG: redo is not required LOG: database system is ready to accept connections LOG: autovacuum launcher started

Source