PG::ConnectionBad: could not connect to server: No such file or directory

PG could not connect to server: No such file or directory

Recently I had this error and I had a little trouble figuring it out so I thought maybe I’d write about it. This error occurred after I updated my Mac OS to the most recent version. In my case that is Mac Catalina 10.15.7.

My first step here was to uninstall the pg gem. gem uninstall pg Then using brew I uninstalled PostgreSQL brew uninstall postgresql then reinstall it brew install postgresql

Next I ran the following commands in my terminal;

ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents This is to have postgresql started at login.

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist This loads postgres.

gem install pg — — with-pg-config=/usr/local/bin/pg_config Reinstalling the PG gem with the homebrew configs.

Rake db:create To create out pg database.

Let’s see what happens…

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”?
Couldn’t create ‘shoppies_development’ database. Please check your configuration.
rake aborted!
PG::ConnectionBad: 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”?

Welp, that didn’t work!

Let me see if I can even start my server! Rails s

=> Run `rails server — help` for more startup options
Puma starting in single mode…
* Version 4.3.7 (ruby 2.6.1-p33), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://127.0.0.1:3000
* Listening on tcp://[::1]:3000
Use Ctrl-C to stop

ok, server has started successfully but I still can’t create my database. Alright, so I will keep researching until I figure this thing out. Next, I think I will restart postgres by running brew services restart postgresql and then try creating my database again. ok, sweet! It looks like I needed to restart postgres for whatever reason and now my database has been created.

If you are having issues such as this I highly recommend taking a look at this blog, shout out to Andrew Ackerman!