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

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

Related

brew pin does not prevent upgrading of postgresql formula

I am trying to prevent my current version of posgtgresql being upgraded. Accordingly I did brew pin postgresql#12. However I am finding that if I do
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
I get the following error message
waiting for server to start.... stopped waiting
pg_ctl: could not start server
Examine the log output.
Looking at the log output, the last lines are
2022-05-01 17:31:00.816 AEST [18650] FATAL: database files are incompatible with server
2022-05-01 17:31:00.816 AEST [18650] DETAIL: The data directory was initialized by PostgreSQL version 12, which is not compatible with this version 14.2.
so postgresql has been updated to version 14.2.
If I do brew list --pinned it shows
postgresql#10
postgresql#11
postgresql#12
so I am still have postgresql#12 pinned. So why has the pinning not worked, and how do I restore posgresql to version 12?
I notice that if I do brew services list I see
postgresql none
postgresql#10 none
postgresql#11 none
postgresql#12 error 6 Chris ~/Library/LaunchAgents/homebrew.mxcl.postgresql#12.plist
i.e. postgresql#14 is not listed, so perhaps postgresql was upgraded in some other manner than using brew? However, I certainly did not do explicit upgrade, so do not see how this could have happened.
In lieu of an answer to this question, the way I recover from this situation is explained in this SO answer.
In brief (warning this destroys the databases)
brew uninstall --force postgresql
brew install postgresql#12
cd /usr/local/var/
rm -rf postgres
initdb /usr/local/var/postgres
Trying to look on the bright side, it is an opportunity to upgrade your databases by either a minor or major version.

5: Input/output error Error: Failure while executing; `/bin/launchctl bootstrap gui/502 and FATAL: password authentication failed for user

The error 5: Input/output Error: Failure while executing; /bin/launchctl bootstrap gui/502 /Users/Andrea/Library/LaunchAgents/homebrew.mxcl.postgresql.plist exited with 5 is occurring when typing in the command "brew services start postgresql".
I am trying to create a database using homebrew and postgresql. If I type the command "brew services stop postgresql" and then start it back up the error does not occur. But will reoccur on the initial start of postgresql.
After, I stop postgresql and restart it using homebrew I type in "createdb 'test'. This error appears:
"error: could not connect to database template1: FATAL: password authentication failed for user "Andrea". Initially I tried to primarily work on the password authentication with no luck.
Here are the steps I have taken:
I have changed the method column to trust in my hba_pg files.
I have tried the command ALTER in the terminal to change the password.
I created a root login on my computer, where I tried to access the file and use the root's password to create the database.
The command chpass to change my user's password.
Help would be much appreciated. Thank you!
This is what I did to resolve on my Mac.
Looked at the postgres.log cat /usr/local/var/log/postgres.log | tail -100
Found the following error on my logs FATAL: lock file "postmaster.pid" already exists
Fix above PID issue and restart the server.
rm /usr/local/var/postgres/postmaster.pid
brew services restart postgresql
Sometimes this can be solved by simply running
brew services restart postgresql
instead of
brew services start postgresql
It happens when a previous postgres process doesn't terminate properly (maybe because you killed a psql process in activity monitor or otherwise). Restarting appears to properly unlock/delete the postmaster.pid file where "start" doesn't.
On the m1 mac I had the same issue and had to remove the postmaster.pid file as well. For m1 macs, the file is at /opt/homebrew/var/postgres/postmaster.pid, so I ran
/opt/homebrew/var/postgres/postmaster.pid
and I could brew services start postgresql successfully.
The other answers didn't apply because I didn't have the pid file (or many other files in those directories). The same error was caused by a bad install, most likely because I had previously installed libpq to get the psql CLI in isolation previously as per: Correct way to install psql without full Postgres on macOS?.
Solution, clean install:
> brew uninstall postgresql
> brew uninstall libpq
> brew install postgresql
> brew services run postgresql
To build on what #gajen-sunthara said, I made sure to upgrade my postgresql installation:
brew services upgrade postgresql
Then removed the postmaster.pid:
rm /usr/local/var/postgres/postmaster.pid
And restarted postgresql:
brew services restart postgresql
And that should get it going again :)
I got the same error:
% brew services start yabai
Bootstrap failed: 5: Input/output error
Try re-running the command as root for richer errors.
Error: Failure while executing; `/bin/launchctl bootstrap gui/503 /Users/myuser/Library/LaunchAgents/
homebrew.mxcl.yabai.plist` exited with 5.
I have an extra user who manages brew called brew.
After trying to run a brew service with sudo, this was a solution (on Apple silicon):
% sudo brew services stop yabai
% sudo chmod -R g+w /opt/homebrew
% sudo chown -R brew /opt/homebrew
% brew services start yabai
I know this has already been answered, but for anyone seeing this in the future, here is some help.
This often happens because it wasn't properly stopped when it was used at a different time, and the version you are using now has a conflict with what you were running previously. There are a few ways to fix it.
Try stoping it with brew services stop postgresql
Start it with brew services start postgresql
You can also just do brew services restart postgresql and not do step 1 and 2. Hope this helps someone!

Brew Postgresql Starts But Process Is Not Running

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

initb fails for brew postgresql-upgrade-database

I am using brew postgresql-upgrade-database to upgrade postgresql from 11.8 to 12 and migrate the data. The process failed with the following output
Upgrading postgresql data from 11 to 12...
Stopping postgresql#11... (might take a while)
==> Successfully stopped postgresql#11 (label: homebrew.mxcl.postgresql#11)
waiting for server to shut down.... done
server stopped
==> Moving postgresql data from /usr/local/var/postgres to /usr/local/var/postgres.old...
==> Creating database...
Error: Upgrading postgresql data from 11 to 12 failed!
==> Moving postgresql data back from /usr/local/var/postgres.old to /usr/local/var/postgres...
Error: Failure while executing; /usr/local/Cellar/postgresql/12.4/bin/initdb --lc-collate=en_US.UTF-8 --lc-ctype=en_US.UTF-8 --lc-messages=en_US.UTF-8 --lc-monetary=en_US.UTF-8 --lc-numeric=en_US.UTF-8 --lc-time=en_US.UTF-8 -E\ UTF8 /usr/local/var/postgres exited with 1.
How do I fix this?
There is an [issue][1] using brew postgresql-upgrade-database. I will update this answer when the issue is resolved.
As at July 21, 2021 this issue is still open. My workaround is below:
WARNING this will wipe out your databases so they have to be reloaded or re-initialized.
brew uninstall --force postgresql
brew install postgresql#12
cd /usr/local/var/
rm -rf postgres
initdb /usr/local/var/postgres
When doing the brew install read the post install message and make sure that you have the correct paths etc in your .zshrc (or equivalent file e.g. .bashrc). If you do not have this set your path correctly, the initdb command may work.
[1]: http://github.com/Homebrew/homebrew-core/issues/60686

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!