'postgresql not installed but homebrew reports it is
I've never had an issue like this before - normally brew install postgres
works and everything is fine.
I've had instances with this machine where I see "Mac OSX restarted due to a problem" and usually there is a stale postmaster.pid
file that I can delete and restart postgresql and everything is fine.
Not any longer - there is something out of whack and I don't know how homebrew works well enough to troubleshoot this.
➜ ~ pg_ctl
zsh: command not found: pg_ctl
➜ ~ brew info postgresql
postgresql: stable 13.1 (bottled), HEAD
Object-relational database system
https://www.postgresql.org/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/postgresql.rb
License: PostgreSQL
==> Dependencies
Build: pkg-config ✔
Required: icu4c ✔, krb5 ✔, [email protected] ✔, 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
==> Analytics
install: 188,386 (30 days), 549,953 (90 days), 1,863,554 (365 days)
install-on-request: 180,714 (30 days), 528,971 (90 days), 1,789,016 (365 days)
build-error: 0 (30 days)
➜ ~ ls -ltra /usr/local/var
total 0
drwxrwxr-x 4 myhandle admin 128 Aug 21 2019 homebrew
drwxr-xr-x 4 myhandle admin 128 Apr 18 2020 log
drwxr-xr-x 15 root wheel 480 Dec 1 17:32 ..
drwxrwxr-x 4 root wheel 128 Dec 2 08:56 .
I have exactly the issue reported here - https://discourse.brew.sh/t/postgress-install-fails-in-postinstall-step/6209
The only answer says "I ran the install script on the old installation" which means nothing to me - I tried brew postinstall postgres@12
imagining this to be what is meant by the "old installation" but I get the exact same result:
➜ ~ brew postinstall postgresql
==> Postinstalling postgresql
Warning: The post-install step did not complete successfully
You can try again using `brew postinstall postgresql`
➜ ~ brew postinstall postgresql@12
==> Postinstalling postgresql@12
Warning: The post-install step did not complete successfully
You can try again using `brew postinstall postgresql@12`
Any ideas?
UPDATE: it's starting to feel like /usr/local/var and /usr/local/bin have gotten confused by either me, homebrew, or the postgres cask because the error messages don't match the reality:
➜ ~ pg_ctl -D /usr/local/var/postgres start
pg_ctl: directory "/usr/local/var/postgres" does not exist
➜ ~ pg_ctl -D /usr/local/bin/postgres start
pg_ctl: could not open PID file "/usr/local/bin/postgres/postmaster.pid": Not a directory
➜ ~ mkdir /usr/local/bin/postgres
mkdir: /usr/local/bin/postgres: File exists
Why does homebrew suggest running pg_ctl from /usr/local/var but postgres process does not exist here after fresh install? Why do I find it in /usr/local/bin ? Then when I try to launch from bin/ it's asking for the postmaster.pid file in bin/ but of course there is no postgres directory in bin/ as these are the processes themselves...
Solution 1:[1]
I've run these steps and postgresql has started working - I'll admit that this is a case of "better lucky than good" because I was just stabbing in the dark here -
? ~ sudo mkdir /usr/local/var/postgres
? ~ sudo chown -R `whoami` /usr/local/var/postgres
? ~ initdb -D /usr/local/var/postgres
The files belonging to this database system will be owned by user "tyler".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /usr/local/var/postgres ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... America/Chicago
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
pg_ctl -D /usr/local/var/postgres -l logfile start
? ~ brew services restart postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
? ~ brew postinstall postgresql
==> Postinstalling postgresql
Success
Then I could get back into Rails-world and migrate the DB properly - I surmise that this means the homebrew installation step (perhaps the postinstall step that kept failing) wasn't creating the /usr/local/var/postgres directory as it should have, or it was trying with insufficient permissions to do so and failing silently.
Once I created the directory manually and chown'd it, things started to work as they should.
Solution 2:[2]
For MacOS: monterey I followed these steps:
Check if postgres is installed or not:
brew list | grep postgres
Initialize DB:
initdb /opt/homebrew/var/postgresql
Start DB:
pg_ctl -D /opt/homebrew/var/postgresql -l logfile start
checking if the db started enter below command:
psql postgres
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | tgmerritt |
Solution 2 | vivek sharmapoudel |