---
title: Debugging a missing PostgreSQL connection on macOS
teaser: 'could not connect to server: No such file or directory. Is the server running
  locally and accepting connections on Unix domain socket?

  '
tags: web,postgresql,macos
author: Mike Burns
published_on: 2020-10-14
---

If you have an Apple laptop that you do development on, or otherwise need to
run a local app on, you might see this error sometimes:

```
could not connect to server: No such file or directory. Is the server running locally and accepting connections on Unix domain socket '/tmp/.s.PGSQL.5432'?
```

This indicates that [PostgreSQL][] is no longer running. If it was previously
working fine, we can fix that. Let's dig in.

[PostgreSQL]: https://www.postgresql.org/

You're going to need [the Terminal app][].

[the Terminal app]: https://support.apple.com/guide/terminal/open-or-quit-terminal-apd5265185d-f365-44cb-8b09-71a064a42125/mac

First up, using [pgrep(1)][] to determine whether Postgres is running:

[pgrep(1)]: https://ss64.com/osx/pkill.html

```sh
% pgrep -l postgres
```

If that gives you no output, that means Postgres isn't running. Great, we can
work with that.

If that _does_ give you output, this blog post isn't for you. You have another
issue than the one we're solving here, and you might have better luck on [Stack
Overflow][].

[Stack Overflow]: https://stackoverflow.com/questions/tagged/postgresql

Let's try starting it using [brew services][]
[brew services]: https://thoughtbot.com/blog/starting-and-stopping-background-services-with-homebrew

```sh
% brew services start postgresql
```

If this command's output included something to the tune of

> Warning: Formula postgresql was renamed to postgresql@14

any further `brew` commands will have to reference postgresql with an included
version number. We can see which version we have installed with:

```sh
% brew list | grep postgresql
```

The rest of the instructions here will use `@14`, but your version might be
different.

After we've started with `brew services,` we'll check to see if it's running:

``` sh
% pgrep -l postgres
```

Still not running? Check the logs with a shell [command sustitution][] (to locate
the proper directory based on our postgres installation) and [tail(1)][]:

[command substitution]: http://zsh.sourceforge.net/Doc/Release/Expansion.html#Command-Substitution
[tail(1)]: https://ss64.com/osx/tail.html

```sh
% tail $(brew --prefix)/var/log/postgresql@14.log
```

You may see an error about a file named `postmaster.pid`:

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

This indicates that Postgres had not been shut down cleanly, for any number of
reasons. The first line of the `postmaster.pid` file is the process identifier
of what once was Postgres. Let's see what that is now, using [ps(1)][]:

[ps(1)]: https://ss64.com/osx/ps.html

```sh
% ps ax | grep $(head -1 /usr/local/var/postgres/postmaster.pid)
```

If you see something about Postgres in the output, then your issue is something
deeper and you'll need to debug more thoroughly before proceeding. But if your
output is empty, or you see something unrelated to Postgres, your <abbr
title="Process IDentifier">PID</abbr> file was
stale and we can remove it.

**Remove the stale PID lock then start the server again**:

```sh
% rm $(brew --prefix)/var/postgresql@14/postmaster.pid
% brew services start postgresql@14
% pgrep -l postgres
```

Postgres should now be running.
