Brew Postgresql Starts But Process Is Not Running
homebrew, macos, postgresql, psql
Solution
Unlike a builtin pkg manger in Linux, Homebrew on macOS doesn't require `root` privilege during most `brew` command. The only exception is `sudo brew services` when you're starting a services listen on port < 1024.
Answer to your question: you messed up file permission with `sudo brew services`.
The solution works for x86 Mac. I've not tested it on M1 mac. (Homebrew stores related files under `/usr/local` on x86 Mac, under `/opt/homebrew` under M1 Mac.)
### Run in Bash or Zsh
# Stop postgresql
sudo brew services stop postgresql
# In case service file is copied to ~/Library/LaunchAgents as well
brew services stop postgresql
# Fix permission of homebrew files
sudo chown -R $USER:admin $(brew --prefix)/*
# Remove socket like `/tmp/.s.PGSQL.5432`
# Restart postgresql without sudo
brew services start postgresql
Besides above fix, you may also need to
- Do a db migration from psql 12 -> psql 13, check `brew info postgresl` for detail
- Start psql manually and check the start log
# Change /usr/local to /opt/homebrew on M1 Mac
# Check the start log
pg_ctl -D /usr/local/var/postgres start
# to stop it
pg_ctl -D /usr/local/var/postgres stop
sudo and brew
I'm not saying `postgresql` can't be started as root, but the Homebrew and `brew services` are not designed to work with `sudo`.
And `sudo brew services` changes some of the files' owner to `root`. You'd better change them back being owned by yourself.
Here's an example (check the Warning part)
❯ sudo brew services start adguardhome
Warning: Taking root:admin ownership of some adguardhome paths:
/usr/local/Cellar/adguardhome/0.106.3/bin
/usr/local/Cellar/adguardhome/0.106.3/bin/AdGuardHome
/usr/local/opt/adguardhome
/usr/local/opt/adguardhome/bin
/usr/local/var/homebrew/linked/adguardhome
This will require manual removal of these paths using `sudo rm` on
brew upgrade/reinstall/uninstall.
Warning: adguardhome must be run as non-root to start at user login!
==> Successfully started `adguardhome` (label: homebrew.mxcl.adguardhome)
`sudo brew services` is not that intelligent enough to change the files' onwer bach to `$USER` automatically.
I've written a script to do this: `brew-fix-perm`. But this is not enough in your case. You have to change back the ownership of postgres configuration files under `$(brew --prefix)/var/postgres`. That's why I put `sudo chown -R $USER:admin $(brew --prefix)/*`.
Related Answer
Similar thing happened that another guy used `sudo brew services` starting MySQL. An additional detailed answer about how `brew services` works is there. You may wanna have a check.
Problem
I have installed Postgres via Brew on a Mac. Then, I have tried to start it ``` | => brew services restart postgres Stopping `postgresql`... (might take a while) ==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql) ==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql) ``` After that, I inspect to see if the process is running ``` (base) ________________________________________________________________________________ | ~/ @ MacBook-Pro (user) | => ps -ef | grep postgres 501 61568 561 0 5:00PM ttys000 0:00.00 grep postgres ``` In addition, `brew services` produces the following ``` | => brew services Name Status User Plist dnsmasq unknown root /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist emacs stopped postgresql error root /Library/LaunchDaemons/homebrew.mxcl.postgresql.plist postgresql@12 stopped rabbitmq stopped unbound stopped ``` Any help on how to troubleshoot this is appreciated.