Search code examples
macospostgresqlhomebrew

Cannot connect to Postgres server running through brew services


I've been looking for a solution for this and could not find a working solution.

I've installed postgres using brew (brew install postgres) in my MacBook and I am currently running it using brew services (brew services list displays postgres as a running service). However, when I try to run psql I get following error.

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

Anyone has already solved similar problem?


Solution

  • I had the same error and I fixed it by removing the process pid file:

    rm -f /usr/local/var/postgres/postmaster.pid

    or for a specific version:

    rm -f /usr/local/var/postgresql@16/postmaster.pid


    [Updated Answer For Arm-based Chips (Apple M1)]

    When you use brew to install postgresql on Apple M1 computers, the postmaster.pid will be located in: /opt/homebrew/var/postgresql/postmaster.pid

    Follow following three steps:

    # 1. Stop PostgreSQL 
    brew services stop postgresql@16
    
    # 2. Delete the postmaster.pid
    rm -f /opt/homebrew/var/postgresql@16/postmaster.pid
    
    # 3. Start the PostgreSQL again
    brew services start postgresql@16
    

    After the above, you can also check the status of the service by brew services info postgresql

    brew services info postgresql@16
    postgresql@16 (homebrew.mxcl.postgresql)
    Running: ✔
    Loaded: ✔
    Schedulable: ✘
    User: root
    PID: 34884