The Data Directory Contains an Old postmaster.pid File
Tags: postgres, database, errors
Published: 2021-01-27 17:39:51

---
title: "The Data Directory Contains an Old postmaster.pid File"
published: true
description: "Manually modifying files within PostgreSQL Data Directory is not recommended as it may lead to data corruption. The data directory contains an old postmaster.pid file error may force you to go against the recommendation in situations where the PostgreSQL server refuses to start."
tags: [ "postgresql", "database", "errors"]
canonical_url: https://kags.me.ke/post/data-directory-contains-an-old-postmaster-pid-file/
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/xmv5o1p0ln8q4p5gvdq3.png
---

## PostgreSQL Connection Failure
Sometimes, computers have a life of their own. You shut down your laptop and on the next boot, you are unable to connect to a PostgreSQL 12 database.

Opening the installed [Postgres.app](https://github.com/PostgresApp/PostgresApp), I notice it displays `stale postmaster.pid file` error.

![Alt="stale postmaster-pid file"](https://dev-to-uploads.s3.amazonaws.com/i/3j2o4vv64y02bcwa748x.png)

## Postmaster.pid
What the heck is the _postmaster.pid_ file? Turns out that the _postmaster.pid_ is a [lock file used to prevent two instances of the same PostgreSQL server from running on the same data-directory](https://www.postgresql.org/docs/current/server-start.html).

>> While the server is running, its PID is stored in the file postmaster.pid in the data directory. This is used to prevent multiple server instances from running in the same data directory and can also be used for shutting down the server.

This is the first time I am encountering such an error. I click on the **Start** button to force a re-start and I get back a dialog with the following message: **The data directory contains a postmaster.pid file, which usually means that the server is already running. When the server crashes or is killed, you have to remove this file before you can restart the server. Make sure that the database process is definitely not running anymore, otherwise your data directory will be corrupted.**

![alt="The data directory contains old postmaster.pid  file"](https://dev-to-uploads.s3.amazonaws.com/i/y8z40p1tz5bqq3xgannm.png)

Click on **OK** button to close the dialog window.

##  Resolving the Problem

1. Open **Postgres.app**
1. Click on **Server Settings...**
1. Click on **Show** button next to the **Data Directory**. This should open the data directory of your PostgreSQl installation.
  ![alt="postgres.app server settings"](https://dev-to-uploads.s3.amazonaws.com/i/h108dlf203gggzv9vksg.png)
1. Locate the _postmaster.pid_ file
  ![alt="postgresql data directory folder"](https://dev-to-uploads.s3.amazonaws.com/i/iwo0dt2kpvwp2ei3rqgp.png)
1. Delete the _postmaster.pid_ file. Right click on the file and select **Move to Bin**
  ![alt="postgresql delete postmaster.pid file"](https://dev-to-uploads.s3.amazonaws.com/i/kuavf8ujo24mijmwrk6h.png)
1. After deleting the file, opening or making the Postgres GUI app window have focus will change the error message from `stale postmaster.pid file` to `Not running`. Click **Start** button on Postgres GUI app to start PostgreSQL server.
![alt="postgresql server  not running error"](https://dev-to-uploads.s3.amazonaws.com/i/matkqku1jw161n452dey.png)

## Way Forward
Why not let PostgreSQL auto remove a stale _postmaster.pid_ file? This would make a user manage a PostgreSQL database without knowing its internal workings. Pros and cons of [auto removing a stale _postmaster.pid_](https://github.com/PostgresApp/PostgresApp/issues/573) exist and balancing between these two is not easy. Therefore, as at the time of this writing, the auto removal of the file remains an open issue and the user's responsibility.

##  Further Reading
- [What's a data directory and postmaster.pid issue #395](https://github.com/PostgresApp/PostgresApp/issues/395)
- [Troubleshooting & Support](https://postgresapp.com/documentation/troubleshooting.html)
- [Don't Manually Modify The PostgreSQL Data Directory!](http://rhaas.blogspot.com/2020/05/dont-manually-modify-postgresql-data.html)
- [Starting the Database Server](https://www.postgresql.org/docs/current/server-start.html)