2

I'm attempting to launch a rails server on big sur (M1 chip) and postgres is giving the following error:

ActiveRecord::ConnectionNotEstablished (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"?
):

I've seen and tried several fixes but none have worked, including the following:

  • Reinstalling postgres via homebrew.
  • Reinstalling the pg gem.
  • brew services restart.
  • Trying to delete a postmaster.pid file (none exist). This directory: "/usr/local/var/postgres/postmaster.pid" does not exist on my machine.

My postgres.log file contains the following line repeating:

could not open directory "pg_notify": No such file or directory LOG: database system is shut down

halfer
  • 19,824
  • 17
  • 99
  • 186
Genetic1989
  • 614
  • 1
  • 6
  • 19
  • 2
    I use https://postgresapp.com/ for development because it saves you the low level hustle. You just start the app and it works. I could not find any explicit information about M1 support, though. – gerrit Mar 14 '21 at 09:51
  • 1
    I had a lot of troubles getting it to work on my M1 and remember to have these exact symptoms. Did a lot os stuff that don't even remember, but I do remember ended creating those missing directories, so try doing `mkdir pg_notify`. After creating the first one, I remember that where a few more directories missing, so did `mkdir` to each of them. What happens when you run `brew services start postgresql`, `brew services list` and `brew info postgres`? Did you succeed doing `gem install pg`? If you give more information, may be I can help you – anonymus_rex Mar 15 '21 at 14:25
  • In the same boat. Dependencies for `pg` come from installing `postgresql@12` (I need 12, not 13). Package manager have any answers? `/usr/local/var` is not the homebrew path, btw. I get the same error. – Rich_F Mar 15 '21 at 17:43
  • @anonymus_rex These are the outputs from those commands: ``` Service `postgresql` already started, use `brew services restart postgresql` to restart. ``` – Genetic1989 Mar 20 '21 at 23:13
  • @anonymus_rex brew services list: kerronking@Kerrons-MacBook-Pro ~ % brew services list Name Status User Plist mysql stopped postgresql error kerronking /Users/kerronking/Library/LaunchAgents/homebrew.mxcl.postgresql.plist – Genetic1989 Mar 20 '21 at 23:14
  • Try the following... do the `mkdir pg_notify` that I told you, after this, run `brew services restart postgresql`, check if it's running or not with `brew services list`, if still doesn't, go check again your postgres log (it should have a different error now). If it says that another directory is missing, do the mkdir for this new directory, and try to restart postgres and so on... – anonymus_rex Mar 21 '21 at 18:45
  • I have all this directories on `/opt/homebrew/var/postgres/`: base/, pg_dynshmem/, pg_notify/, pg_snapshots/, pg_subtrans/, pg_wal/, global/, pg_logical/, pg_replslot/, pg_stat/, pg_tblspc/, pg_xact/, pg_commit_ts/, pg_multixact/, pg_serial/, pg_stat_tmp/, pg_twophase/ – anonymus_rex Mar 21 '21 at 18:45
  • This did not fix the issue. The error on the console remained the same and the error on the log remained the same as well @anonymous_rex – Genetic1989 Mar 30 '21 at 01:35

3 Answers3

3

While Genetic's answer works, a quicker solution would be to delete the partially created database (assuming you have just installed postgres and there's no data to be lost) and then run initdb as listed in brew info postgresql to recreate the database:

brew services stop postgresql
rm -rf "$(brew --prefix)/var/postgres"
initdb --locale=C -E UTF-8 "$(brew --prefix)/var/postgres"
brew services start postgresql
dev
  • 1,648
  • 16
  • 25
2

The original error on the console didn't change until I entered the following command:

brew services restart -vvv postgresql

After doing this, the errors updated. It then displayed the other directories and sub-directories that were missing. Once I added everything, all was fine.

Genetic1989
  • 614
  • 1
  • 6
  • 19
  • Could you elaborate Genetic? Did you see the updated errors in the log file or your rails server? What directories did you add and where did you add them. Really appreciate any help you can provide. – nflauria Jun 15 '21 at 19:00
1

The solution by anonymus_rex in the comments worked for me. Here are the exact steps I needed to take in case it could help anyone to elaborate a bit more. i was stuck on this for way too long.

I tried almost all of the answers in this question and this other one and this is what finally worked for me to get postgres to start

  • tail the logs for postgres.
    • the path needs to be updated depending on where postgres is installed, and your version. I am using postgresql@14 on an m1 Monterey and installed it with homebrew.

    • i finally found the path i needed to look at using this article.

    • tail /opt/homebrew/var/log/postgresql@14.log

output shows this:

2023-02-03 15:33:49.294 CST [82651] FATAL:  could not open directory "pg_notify": No such file or directory
2023-02-03 15:33:49.294 CST [82651] LOG:  database system is shut down
  • go to the / directory and cd opt/homebrew/var/postgresql@14

  • create the missing directory (maybe this is a different directory for you)

    • mkdir pg_notify
  • repeat this process for all missing directories.

    • I needed to mkdir for pg_tblspc, pg_replslot, pg_twophase, pg_stat_tmp, pg_logical/snapshots, pg_logical/mappings, pg_commit_ts, pg_snapshots, & pg_commit_ts but i recommend you specifically run the tail command each time to make sure you are not missing different directories & files than me.

finally after running the tail command repeatedly after creating each missing directory, I got this output.

2023-02-03 15:49:18.909 CST [85772] LOG:  redo done at 0/17211D8 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s
2023-02-03 15:49:18.914 CST [85771] LOG:  database system is ready to accept connections

i was then able to create & migrate my db in my project ・ᴗ・