Restarting Postgres for Rails on MacOS
Last night I encountered this error message while building Ranting:
1
2
3
ActiveRecord::ConnectionNotEstablished (connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?
):
I vaguely remember encountering this error message a while back 🤔. I’m not certain what actually caused the error, however I figured restatrting Postgress would solve this. Here’s how to do it.
1. Look for Postgres Installation
My current setup has Postgres installed with Homebrew.
1
2
3
4
5
6
7
$ brew list
==> Formulae
...
cairo liblqr poppler
coreutils libnghttp2 postgresql@14
docbook libomp protobuf
...
Seems that I have postgresql@14
installed.
2. Restart Postgres
Now, navigate to this folder:
1
$ cd /usr/local/var
Look for the same Postgres installation folder as in Homebrew list above.
1
2
3
$ ls
cache homebrew mysql run
db log postgresql@14
cd
into the Postgres folder and look for a file called postmaster.pid
.
1
$ cd postgresql@14
1
2
3
4
5
6
7
8
$ ls
PG_VERSION pg_ident.conf pg_snapshots pg_wal
base pg_logical pg_stat pg_xact
global pg_multixact pg_stat_tmp postgresql.auto.conf
pg_commit_ts pg_notify pg_subtrans postgresql.conf
pg_dynshmem pg_replslot pg_tblspc postmaster.opts
pg_hba.conf pg_serial pg_twophase postmaster.pid
Found it! Next, backup the postmaster.pid
file to another folder (eg. the Desktop folder) and restart Postgres.
1
$ mv postmaster.pid ~/Desktop
1
2
3
4
$ brew services restart postgresql@14
Stopping `postgresql@14`... (might take a while)
==> Successfully stopped `postgresql@14` (label: homebrew.mxcl.postgresql@14)
==> Successfully started `postgresql@14` (label: homebrew.mxcl.postgresql@14)
3. Move On with Life
Run Rails server again and that should resolve the error.
It should be safe now to delete the postmaster.pid
that was backed up earlier.