Brew Postgresql Starts But Process Is Not Running - postgresql

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.

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
I've known a stubborn guy stick with sudo brew services to start mysql. And a more detailed answer about brew services is there. You may wanna have a check.

Short answer:
brew services stop postgresql
sudo chown -R $(whoami) $(brew --prefix)/*
brew services start postgresql

Related

Error: Failure while executing; `/bin/launchctl bootstrap gui/501 /Users/josh/Library/LaunchAgents/homebrew.mxcl.postgresql#14.plist` exited with 5

When trying to start postgresql
➜ ~ brew services start postgresql
Warning: Use postgresql#14 instead of deprecated postgresql
Bootstrap failed: 5: Input/output error
Try re-running the command as root for richer errors.
Error: Failure while executing; `/bin/launchctl bootstrap gui/501 /Users/josh/Library/LaunchAgents/homebrew.mxcl.postgresql#14.plist` exited with 5.
Getting "error" as status when running brew services list
➜ ~ brew services list
Name Status User File
postgresql#14 error 256 root ~/Library/LaunchAgents/homebrew.mxcl.postgresql#14.plist
PSQL was working perfectly fine, shut down my laptop (did not update) and when I turned it on the next day psql was not working. I am on OSX Version 12.6 (Monteray).
Had the same error today 🤓 on my Mac Ventura 13.0. Did these steps and it helped.
Run rm /usr/local/var/postgresql#{YOUR_VERSION}/postmaster.pid to remove postmaster.pid file. this is usually caused by an error in the postmaster.pid file.
Run brew services stop postgresql to stop the current postgresql service.
Then finally run brew services start postgresql, a new postmaster.pid file will be generated.
If this doesn't work you can run brew doctor and see if it will return Your system is ready to brew. If there are warnings 🥶, you can share them here with the community for more details.?
I had the same issue on 13.0 on M1 pro, all is did was brew services restart postgresql#14, it shut down, restarted and ran fine, issue gone.
Had the same issue, but couldn't get Postgres 14 working, and ended up just upgrading to Postgres 15, which I did get to work.
The steps looked like this (version numbers might vary)
brew services stop postgresql#14
brew install postgresql#15
# This command will copy data over to the new database
brew /opt/homebrew/Cellar/postgresql#15/15.1/bin/pg_upgrade -b /opt/homebrew/Cellar/postgresql#14/14.6/bin/ -d /opt/homebrew/var/postgres/ -D /opt/homebrew/var/postgresql#15/
brew services start postgresql#15
In my case, I had postgresql#13 and postgresql#14 install with brew on Ventura 13.1
I first removed postgresql#13 and all data within :
brew remove postgresql#13 and rm -rf /usr/local/var/postgresql#13
This assure me that the previous folder is fully remove, however, I did not care about my local data ... you may want to take care of it before doing this
After that, I reinstall postgresql#14 within the following commands :
# Remove postgresql 14 with brew
brew remove postgresql#14
# Reinstall postgresql 14 with brew
brew install postgresql#14
# Remove all the files in the db folder
rf -rf /usr/local/var/postgresql#14/*
# Kill all process that run any db of the postgresql 14 folder
pkill -f /usr/local/var/postgresql#14
# Initialize the db folder for postgresql 14
initdb --locale=C -E UTF-8 /usr/local/var/postgresql#14
# Restart postgresql with brew (should say that it's already running)
brew services start postgresql#14

Postgres does not know where to find the server configuration file. After "brew install postgresql"

I have installed PostgreSQL on MacOS Big Sur version 11.3 with brew install postgresql.
When I run the postgres command I get this error
postgres does not know where to find the server configuration file.`
You must specify the --config-file or -D invocation option or set the PGDATA environment variable.
Where do I find the config file?
Checking from the package formula postgresql.rb by brew cat postgresql, the datadir is ${HOMEBREW_PREFIX}/var/postgres. (HOMEBREW_PREFIX is not an exported env var, it's used by homebrew internally)
/usr/local/var/postgres on x86 Mac
/opt/homebrew/var/postgres on M1 Mac
This dir should be created automatically during brew install postgresql.
BTW, the canonical way to start psql with Homebrew should be brew services start postgresql, which uses launchd on macOS to manage services. (For newbie on macOS, launchd could be seen as a systemd alternative on macOS.)
Run brew info postgresql and some additional tips is displayed in the Caveats part of the output.
❯ brew info postgresql
postgresql: stable 13.3 (bottled), HEAD
Object-relational database system
https://www.postgresql.org/
/usr/local/Cellar/postgresql/13.3 (3,225 files, 38.8MB) *
Poured from bottle on 2021-05-14 at 09:37:24
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/postgresql.rb
License: PostgreSQL
==> Dependencies
Build: pkg-config ✔
Required: icu4c ✔, krb5 ✔, openssl#1.1 ✔, readline ✔
==> Options
--HEAD
Install HEAD version
==> Caveats
To migrate existing data from a previous major version of PostgreSQL run:
brew postgresql-upgrade-database
This formula has created a default database cluster with:
initdb --locale=C -E UTF-8 /usr/local/var/postgres
For more details, read:
https://www.postgresql.org/docs/13/app-initdb.html
To have launchd start postgresql now and restart at login:
brew services start postgresql
Or, if you don't want/need a background service you can just run:
pg_ctl -D /usr/local/var/postgres start
On my Mac (Big Sur), it's located at /usr/local/var/postgres/postgresql.conf. The command I use to start the server is:
pg_ctl -D /usr/local/var/postgres start

Postgresql#12 on Big Sur Won't Start

Installed postgresql#12 and for some reason won't start:
/opt/homebrew/var/postgresql#12 stable ❯ pg_ctl -D /opt/homebrew/opt/postgresql#12/bin/postgres start
pg_ctl: could not open PID file "/opt/homebrew/opt/postgresql#12/bin/postgres/postmaster.pid": Not a directory
/opt/homebrew/var/postgresql#12 stable ❯ brew services list
Name Status User Plist
postgresql#12 error rich /Users/rich/Library/LaunchAgents/homebrew.mxcl.postgresql#12.plist
~ ❯ sudo su - postgres
su: unknown login: postgres
~ ❯ brew services start postgresql#12
Service `postgresql#12` already started, use `brew services restart postgresql#12` to restart.
~ ❯ brew services restart postgresql#12
Stopping `postgresql#12`... (might take a while)
==> Successfully stopped `postgresql#12` (label: homebrew.mxcl.postgresql#12)
==> Successfully started `postgresql#12` (label: homebrew.mxcl.postgresql#12)
~ ❯ sudo su - postgres
su: unknown login: postgres
Not sure why there isn't a postgres user created with the install.
Anybody know what I can do to get this back in line? Cheers

The problem when installing mongodb on MacOS

I tried to install mongodb on my Mac. But the error message below appeared.
Someone please tell me the solution.
Updating Homebrew...
==> Installing mongodb-community from mongodb/brew
==> Downloading https://fastdl.mongodb.org/osx/mongodb-macos-x86_64-4.4.1.tgz
Already downloaded: /Users/t17akeru/Library/Caches/Homebrew/downloads/d46781539dca95a7ced7d9b466b13bd4cc0202f17a8bf4bd499a5e1a4a241262--mongodb-macos-x86_64-4.4.1.tgz
Error: Failed to install plist file
Warning: The post-install step did not complete successfully
You can try again using `brew postinstall mongodb/brew/mongodb-community`
==> Caveats
To have launchd start mongodb/brew/mongodb-community now and restart at login:
brew services start mongodb/brew/mongodb-community
Or, if you don't want/need a background service you can just run:
mongod --config /usr/local/etc/mongod.conf
==> Summary
🍺 /usr/local/Cellar/mongodb-community/4.4.1: 11 files, 136.8MB, built in 4 seconds
==> `brew cleanup` has not been run in 30 days, running now...
Error: Permission denied # apply2files - /usr/local/share/5.2/luarocks/add.lua
MongoDB was correctly installed. The problem is the installation of the plist file (which allows you to launch MongoDB when your computer starts, for example).
You can still run MongDB using mongod --config /usr/local/etc/mongod.conf.
Also, brew seems to have some permission problem. Here are some possible resolutions from this GitHub issue:
Reinstalling Brew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Resetting permissions in /usr/local
sudo chown -R $(whoami):admin /usr/local/* \
&& sudo chmod -R g+rwx /usr/local/*
You can then try to run MongoDB's post-install script using brew postinstall mongodb/brew/mongodb-community
Hope it helped !

how to fix postgres after updating/upgrading brew

I upgraded to mavericks and had some trouble installing/compiling new gems so I reinstalled xcode and did a brew update and upgrade. Gems work now, and even postgres continued to work for a while until a recent reboot. Now postgres seems to be having issues.
postgres:
postgres does not know where to find the server configuration file.
You must specify the --config-file or -D invocation option or set the PGDATA environment variable.
brew info postgres:
postgresql: stable 9.3.2 (bottled)
http://www.postgresql.org/
Conflicts with: postgres-xc
/usr/local/Cellar/postgresql/9.2.4 (2842 files, 39M)
Built from source
/usr/local/Cellar/postgresql/9.3.2 (2924 files, 39M) *
Poured from bottle
postgres -D /usr/local/var/postgres:
FATAL: database files are incompatible with server
DETAIL: The data directory was initialized by PostgreSQL version 9.2, which is not compatible with this version 9.3.2.
What should I do now to get my database working again?
$ brew tap homebrew/versions
$ brew install postgresql92
$ brew switch postgresql 9.2.8 # might need to check this one, could be newer by the time you read this
$ pg_dumpall > ninedottwo-dump
$ brew uninstall postgresql92
$ brew switch postgresql 9.3.4 # again, check version
$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
$ createdb # i got an error about my database not existing, so i had to run this
$ psql < ninedottwo-dump
$ bundle exec rails s
Thanks to Peter for getting me started in the right direction.
edit: this'll teach me to upgrade everything all at once...
You need to get a copy of PostgreSQL 9.2 installed somehow, to be able to access the existing data directory.
Options for installing 9.2 via Homebrew include:
Get a checkout of Homebrew from before the postgresql formula was upgraded to 9.3.
Install postgresql92 from homebrew/versions tap.
Install postgresql-9.2 from petere/postgresql tap (disclosure: that's my project).
Install from source.
Then you ought to upgrade that to 9.3 using either pg_dump or pg_upgrade.
pg_upgrade comes for this very purpose, and using it is straightforward. Just make sure you're specifying the right paths for the old data and binaries ( -d and -b ) and the right paths to the new data and binaries (-D and -B):
pg_upgrade \
-d /usr/local/var/postgres/9.2/ -D /usr/local/var/postgres/9.3/ \
-b /usr/local/Cellar/postgresql/9.2.4/bin/ -B /usr/local/Cellar/postgresql/9.3.2/bin/
If everything goes fine, you can then cleanup old versions with brew cleanup postgresql,
and since a new version of PostgreSQL means new versions of the native libraries that are used by the Ruby pg gem, you may want to recompile it:
gem uninstall pg
ARCHFLAGS="-arch x86_64" gem install pg
For 9.2 to 9.5, on El Capitan, I ran into a couple problems which might help others.
It seems now the name of the package used in the switch statement has changed to include the version number (postgresql92 vs postgresql). So, I needed to have the added step of unlinking the current version of postgres:
brew unlink postgresql
brew link postgresql92
brew switch postgresql92 9.2.13 # match version
In order to perform the dump I needed to start the postgres server,
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
But then I ran into the dreaded:
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"?
I check out the logs and found this:
could not open directory "pg_tblspc": No such file or directory
According to this answer by Donovan, Yosemite and El Capitan remove some needed directories. So I also needed to add those directories back in using this awesome command by Nate
mkdir /usr/local/var/postgres/{pg_tblspc,pg_twophase,pg_stat,pg_stat_tmp,pg_replslot,pg_snapshots}/
So the full updated version is:
brew tap homebrew/versions
brew install postgresql92
brew unlink postgresql
brew switch postgresql92 9.2.13 # might need to check this one, could be newer by the time you read this
mkdir /usr/local/var/postgres/{pg_tblspc,pg_twophase,pg_stat,pg_stat_tmp,pg_replslot,pg_snapshots}/
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
pg_dumpall > ninedottwo-dump
pg_ctl -D /usr/local/var/postgres stop
brew unlink postgresql92
brew uninstall postgresql92
brew switch postgresql 9.5.2 # again, check version
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
createdb # i got an error about my database not existing, so i had to run this
psql < ninedottwo-dump
bundle install # need to make sure you have pg gem for 9.5
bundle exec rails s
All credit goes to sentinel, Nate, and Donovan. Thanks so much!