brew services start postgresql@14 がこけた

## 現象

```sh
$ brew services start postgresql@14

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/my_pc/Library/LaunchAgents/homebrew.mxcl.postgresql@14.plist` exited with 5.
```

## 修理の過程

とりあえず、`/Users/my_pc/Library/LaunchAgents/homebrew.mxcl.postgresql@14.plist`を見てみる。

```html
$ cat /Users/my_pc/Library/LaunchAgents/homebrew.mxcl.postgresql@14.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>KeepAlive</key>
	<true/>
	<key>Label</key>
	<string>homebrew.mxcl.postgresql@14</string>
	<key>LimitLoadToSessionType</key>
	<array>
		<string>Aqua</string>
		<string>Background</string>
		<string>LoginWindow</string>
		<string>StandardIO</string>
		<string>System</string>
	</array>
	<key>ProgramArguments</key>
	<array>
		<string>/opt/homebrew/opt/postgresql@14/bin/postgres</string>
		<string>-D</string>
		<string>/opt/homebrew/var/postgresql@14</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
	<key>StandardErrorPath</key>
	<string>/opt/homebrew/var/log/postgresql@14.log</string>
	<key>StandardOutPath</key>
	<string>/opt/homebrew/var/log/postgresql@14.log</string>
	<key>WorkingDirectory</key>
	<string>/opt/homebrew</string>
</dict>
</plist>
```

`StandardErrorPath = /opt/homebrew/var/log/postgresql@14.log` とのことなので、これを見てみる。


```ruby
$ cat /opt/homebrew/var/log/postgresql@14.log

2022-10-31 14:44:29.831 JST [13709] LOG:  database system is shut down
2022-10-31 14:44:39.881 JST [13962] LOG:  starting PostgreSQL 14.5 (Homebrew) on aarch64-apple-darwin21.6.0, compiled by Apple clang version 14.0.0 (clang-1400.0.29.102), 64-bit
2022-10-31 14:44:39.883 JST [13962] LOG:  listening on IPv6 address "::1", port 5432
2022-10-31 14:44:39.883 JST [13962] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2022-10-31 14:44:39.883 JST [13962] FATAL:  could not open lock file "/tmp/.s.PGSQL.5432.lock": Permission denied
```

/tmp/.s.PGSQL.5432.lock を見る際のPermission errorみたいなので、sudoで実行してみる。

```sh
$ sudo brew services start postgresql@14                                      

Warning: Taking root:admin ownership of some postgresql@14 paths:
  /opt/homebrew/Cellar/postgresql@14/14.8/bin
  /opt/homebrew/Cellar/postgresql@14/14.8/bin/postgres
  /opt/homebrew/opt/postgresql@14
  /opt/homebrew/var/homebrew/linked/postgresql@14
This will require manual removal of these paths using `sudo rm` on
brew upgrade/reinstall/uninstall.
Warning: postgresql@14 must be run as non-root to start at user login!
==> Successfully started `postgresql@14` (label: homebrew.mxcl.postgresql@14)
```

brew servicesで確認すると失敗していた。
改めてログを見ると、


```sh
$ cat /opt/homebrew/var/log/postgresql@14.log

The server must be started under an unprivileged user ID to prevent
possible system security compromise.  See the documentation for
more information on how to properly start the server.
"root" execution of the PostgreSQL server is not permitted.
```

sudoでpostgresql起動はできないっぽい。

一旦、`/tmp/.s.PGSQL.5432.lock`を手動削除してみた。

```sh
$ rm /tmp/.s.PGSQL.5432.lock             

override rw------- postgres/wheel for /tmp/.s.PGSQL.5432.lock? y
```

これで再実行するとうまくいった。🎉

```sh
$ brew services start postgresql@14     
==> Successfully started `postgresql@14` (label: homebrew.mxcl.postgresql@14)
$ brew services                   
Name          Status  User File
postgresql@14 started root 
```