Search code examples
postgresqlpsqlmacos-sonoma

psql error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory


I was installing postgres on macos sonoma (apple M1 chip) and after installing, then starting the service and finally running psql in the terminal I ran into this error

psql: error: 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?

Steps taken to arrive at this error:

  1. $ brew install postgresql
  2. $ brew services start postgresql

When I run $ brew services list there's this error

screenshot of brew services list showing failed postgresql service with error

Didn't find a specific answer but I resolved the error as below (in the answer)


Solution

  • To install postgresql:

    1. $ brew install postgresql this installs the latest stable version of postgresql available
    2. Run initdb to initialize a database cluster $ initdb /opt/homebrew/var/postgresql@14
    3. Start (or restart) the postgres service $ brew services start postgresql

    Running $ brew services list should show a successful running instance (shown in screenshot below)

    screenshot of brew services list showing services successfully started

    TIP: To see the errors logged when running the service you can open the logs stored in [email protected]

    $ vi /opt/homebrew/var/log/[email protected]
    

    (That's how I found the specific error I was seeing when listing the service and a potential solution) screenshot of the log of errors

    Go to initdb documentation