PostgreSQL permissions issue after upgrading to OS X Lion - macos

After upgrading to Lion, I get the following error when trying to start up the Postgres server:
pg_ctl: could not open PID file "/usr/local/var/postgres/postmaster.pid": Permission denied
I also tried to re-run the initdb command, but ran into a similar problem:
initdb: could not access directory "/usr/local/var/postgres": Permission denied
If it matters, PostgreSQL was installed via Homebrew. Running brew info postgresql yields the expected results (version, summarized docs).

Well, it turns out the solution was pretty simple. I changed the group on /usr/local/var to staff (from wheel) and changed the ownership (chown -R) to my system account (from root).
After that, postgres started up fine.
I was a little nervous changing those permissions, but the only thing in my /usr/local/var was a postgres directory, so all should be well. If you have other directories/files in /usr/local/var, maybe don't use the -R flag when chown'ing?
The Homebrew ruby installer script changes the group of /usr/local/var to staff, so that must have gotten undone when upgrading to Lion. Not sure about the ownership being root instead of my system account though...

sudo chown -R $(whoami) /usr/local/var

Related

Fix Home Brew permissions on High Sierra

I accidently tried renamed my home folder on my mac. Never. Ever. Do. That! Homebrew no longer works... I get this error no matter what I try to install (or another github address).
Error: Permission denied # rb_sysopen - /private/tmp/github_api_headers20180921-5820-13099yc.
Nothing works, not even wget or mysql.
I tried uninstalling / re-installing but doesn't work.
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
How can I fix homebrew? Is it necessary to wipe the whole computer?
Files under /Users/myuser should have ownership to myuser and group ownership to staff.
I don't know your specific settings but before to wipe out your system you could try to restore these basic ownerships with:
chown -R myuser:staff /Users/myuser
where myuser is the name of your user. As I said, I don't know anything about your specific settings so do this at your own risk.

initdb /usr/local/var/postgres on El Capitan

I'm attempting to run initdb /usr/local/var/postgres after installing postgresql 9.5 via homebrew on El Capitan but I get the following:
The files belonging to this database system will be owned by user "kyledecot".
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 ... initdb: could not change permissions of directory "/usr/local/var/postgres": Operation not permitted
When attempting to cd /usr/local/var/postgres I get:
cd: permission denied: /usr/local/var/postgres
It appears that I don't have the proper permission to do this. I assume this has something to do w/ El Capitan's System Integrity Protection but I could be wrong.
How do I go about fixing this?
sudo chown -R `whoami` /usr/local
I had to combine two bits of advice that I have seen with Stack Overflow questions related to this question/error. I found myself in a chicken and an egg situation where I would reinstall postgres and change permissions one at a time, but both commands conflicted with each other.
So I did this:
sudo chown -R `whoami` /usr/local && brew postinstall postgresql
...and it worked fine.
I find it not really efficient and secure to chown all /usr/local directory, so I did simply
mkdir /usr/local/var/postgres
And all went smooth.

Homebrew postgres broken

I installed Postgresql 9.4.0 installed on my Mac (10.10.1/Yosemite) using homebrew. It does not work.
I have created the softlink to /usr/local/opt/postgresql/homebrew.mxcl.postgresql.plist in ~/Library/LaunchAgents.
If I try to manually load postgres I get the message that the "Operation is in progress"
> launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
/usr/local/Cellar/postgresql/9.4.0/homebrew.mxcl.postgresql.plist: Operation already in progress
However postgres does not appear to be running.
> ps auxw | grep post
billmcn 670 0.0 0.0 2424272 452 s000 R+ 10:12PM 0:00.01 grep post
and I cannot connect with the command line client.
> psql
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"?
To my knowledge I have tried all the fixes suggested on other Stackoverflow threads discussing this problem. Specifically:
I have uninstalled and reinstalled postgres and the accompanying Ruby gem. There is no postgres 8.0 version on my machine.
I have verified that the psql client program is the 9.4.0 version installed by Homebrew and not a Mac system binary.
I have verified that the /usr/local/var/postgres/postmaster.pid does not exist.
I have rebooted the machine.
I did have Homebrew postgres working on this machine earlier. I think what broke it is upgrading from version 8 to version 9 but I'm not sure.
I don't have any databases I need to preserve. I'm willing to start clean with postgres; I just need to get it to work now. Any ideas?
The issue appears to have been permissions on the /usr/local/var/postgres directory. Here is what my var directory looked like when things weren't working.
ll /usr/local/var/
drwxr-xr-x 3 billmcn admin 102 Dec 20 12:44 cache
drwxr--r-- 2 root admin 68 Dec 29 21:37 postgres
(whoami = "billmcn")
I deleted /usr/local/var/postgres, uninstalled and reinstalled postgres, and now it looks like this.
ll /usr/local/var/
drwxr-xr-x 3 billmcn admin 102 Dec 20 12:44 cache
drwx------ 23 billmcn admin 782 Dec 30 10:51 postgres
Not sure how it got into this state because I don't remember futzing with the permissions on this directory, but no matter. It works now.
I had the same problem installing postgres using homebrew on a freshly installed Yosemite.
First off my brew config looks like this:
HOMEBREW_VERSION: 0.9.5
ORIGIN: https://github.com/Homebrew/homebrew
HEAD: 9f6926265f8e4be7cc80dfe9042f2cd3c1e8dc9e
Last commit: 64 minutes ago
HOMEBREW_PREFIX: /usr/local
HOMEBREW_CELLAR: /usr/local/Cellar
CPU: quad-core 64-bit sandybridge
OS X: 10.10.1-x86_64
Xcode: 6.1.1
Clang: 6.0 build 600
X11: N/A
System Ruby: 2.0.0-481
Perl: /usr/bin/perl
Python: /usr/bin/python
Ruby: ~/.rvm/rubies/ruby-2.1.1/bin/ruby
First thing i noticed was that I had no write permission to /usr/local/var/postgres. This was easily changed issuing sudo chown -R `whoami` /usr/local/var/postgres then I reinstalled postgresql and did
cat /usr/local/var/postgres/server.log
which revealed:
postgres cannot access the server configuration file "/usr/local/var/postgres/postgresql.conf": No such file or directory
So I removed the directory /usr/local/var/postgres and issued the command to initialize the database.
initdb -D /usr/local/var/postgres/
This seemed to have done the trick and postgres is running fine.
I had this same problem. The primary issue here is that the initdb step of installation will create the directory with root ownership instead of as the user on a Mac. To solve this issue:
Create the data directory before running initdb and set permissions of 0700
rm -rf /usr/local/var/postgres # in case this is not your first try
mkdir /usr/local/var/postgres
chmod 0700 /usr/local/var/postgres
Then run initdb and it will respect the permissions of the data directory.
initdb -D /usr/local/var/postgres
For grins and giggles, create a test db named after your user:
createdb `whoami`
Login to test:
psql
After trying to install postgresql with Homebrew, I got this:
Warning: postgresql-9.5.2 already installed, it's just not linked
So I tried:
brew link postgresql
And got this error:
Linking /usr/local/Cellar/postgresql/9.5.2...
Error: Could not symlink share/man/man3/SPI_connect.3
/usr/local/share/man/man3 is not writable.
It seemed to be a write permission matter, so I did:
sudo chown -R `whoami` /usr/local/share/man/
It did the trick because, then I was able to do (without error):
brew link postgresql
In case anyone upgraded from a previous version, dont forget to:
brew postgresql-upgrade-database
That will solve the problem by upgrading your existing databases to the version you upgraded postgres to.
Please note that their is a thread on Homebrew's github dealing with this issue: https://github.com/Homebrew/homebrew/issues/35240
I have had a similar issue. James answer helped me solve it. But I then ran into the issue jbk is mentioning (after having deleted /usr/local/var/postgres, it kept on being recreated).
The issue is that if you have created the symlink:
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
and launched the process:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
you should first unload it:
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
before running James's commands.
rm -rf /usr/local/var/postgres # in case this is not your first try
mkdir /usr/local/var/postgres
chmod 0700 /usr/local/var/postgres
In addition, if, like me, you have an admin user managing homebrew and a regular user who will be using pgsl for development purpose, James command should be run as super user:
sudo -s
and ownership over the postgres directory should be given to your dev user:
chown my-dev-user /usr/local/var/postgres
The following command, run as the dev user, should then properly populate the directory:
createdb `whoami`
Running:
psql -l
should show you the tables and user permissions in postgre after such manipulations.
Hope this helps.
I had to delete the .pid file after seeing this in the logs
/usr/local/var/log/postgres.log
2021-10-10 19:05:27.468 BST [41868] FATAL: lock file "postmaster.pid" already exists
2021-10-10 19:05:27.468 BST [41868] HINT: Is another postmaster (PID 820) running in data directory "/usr/local/var/postgres"?
rm /usr/local/var/postgres/postmaster.pid
I installed it using brew
based on #James answer this is what I did on my M1 Monterey machine. For me the directory differed.
DANGER: In the comments it has been pointed out that my script deletes the database.
In terminal:
#to fix postgresql of existing installation
cd /opt/homebrew/var
rm -rf postgres
mkdir postgres
chmod 0700 postgres
initdb -D postgres
#install postgres
echo "installing postgres..."
brew install postgresql
brew services restart postgresql
createuser postgres -s
I then could brew install --cask pgadmin4 and run pgadmin from Applications and connect to 127.0.0.1.
I recently had a problem which began when I upgraded some brew updates / upgrades, mainly python versions etc. What worked for me.
brew uninstall postgres
brew install postgresql#9.5
echo 'export PATH="/usr/local/opt/postgresql#9.5/bin:$PATH"' > ~/.zshrc
# you may need > ~/.bashrc if you use bash
I needed pg_dump, pg_restore etc so to get that working I did
brew install libpq
Start the service
brew services start postgresql#9.5
From here I would have expected everything to work but still all rails db commands were giving error that server was not running. This final bit was the missing piece of the puzzle which finally solved it for me.
gem uninstall pg
gem install pg -v 0.20.0 # which was set in Gemfile
# could also just probably do bundle install instead.
For posterity, I had this issue and wanted to note what worked for me.
I am running postgres 11.2 on High Sierra. I had recently upgraded from postgres 10 with brew postgresql-upgrade-database.
I kept getting the error psql: could not connect to server: No such file or directory, and my server.log indicated is another postmaster (PID 5894) running in data directory "/usr/local/var/postgres"?
I tried several solutions including restarting my computer, deleting postmaster.pid, using brew services restart postgres, but to no avail. I eventually stumbled on the solution:
brew unlink postgresql && brew link postgresql
No idea why this worked, but putting it here mostly so I can reference it myself in the future! Throw stuff at the wall till it sticks!
Check #leo_chaz_maltrait for fixing errors the error Could not symlink share/man/man3/SPI_connect.3
Another error that might show up is:
Error: Could not symlink lib/pkgconfig/libecpg.pc
sudo chown -R `whoami` /usr/local/lib/pkgconfig
brew link postgresql
Please read and follow the instructions.
Check postgres logs to see what the issue is.
tail -f /usr/local/var/log/postgres.log
tail -f /opt/homebrew/var/log/postgres.log
tail -f /usr/local/var/postgres/server.log
In my case it this was the error.
2022-07-19 21:16:12.095 IST [2138] FATAL: data directory "/usr/local/var/postgres" has invalid permissions
[3472] FATAL: lock file "postmaster.pid" already exists
Added the required permission and issue got fixed.
sudo chown -R vikas /usr/local/var/postgres
rm /usr/local/var/postgres/postmaster.pid
That's it.
I'd this issue after shutting down the computer due power outage.
# This initialize your database with the current data and settings
initdb -D postgres
# This will start database service
pg_ctl -D postgres -l logfile start

Changing Ownership of a directory in OS X

I've installed homebrew, and am trying to change the write permissisons for the /usr/local/include directory.
When I run 'brew doctor', I get this error message:
Error: The /usr/local directory is not writable. Even if this
directory was writable when you installed Homebrew, other software may
change permissions on this directory. Some versions of the "InstantOn"
component of Airfoil are known to do this.
You should probably change the ownership and permissions of /usr/local
back to your user account.
I tried doing that with chown, but I'm pretty new at this and don't think I was running it correctly. I ran:
chown myusername /usr/local/include
I didn't get any error message, but when I run brew doctor it says I still lack permission to write to /usr/local/include.
Any help would be greatly appreciated!
Edit:
I'm getting an "operation not permitted" error.
cd /usr
chown myusername local
chown: local: Operation not permitted
Simple solution that worked for me:
click on your background to go to finder
click on go and go to folder /usr
right click on local and do get info
unlock the lock at the bottom
click + sign and add your user to the list and give read/write privileges
click on the gear sign at the bottom and choose apply to enclosed items to recurse under that directory and assign privileges too all directories beneath it.
type brew doctor from command prompt to test. My result:
Your system is ready to brew.
On my system, /usr/local is owned by root:admin and is rwxrwxr-x. My user is a member of the admin group, thus has write permissions. I haven't messed with ownership and permissions there, and my Homebrew installation is not complaining, so I assume my setup fits its requirements.
Check the ownership of your /usr/local; if it is owned by group admin, check if your non-adminsitrator account is a member of that group.
You should use: sudo chown username folder
Be careful with the change of ownership
Do not try to change ownership for /usr/ or recursively for /usr/local/
if you are not getting the same error posted in the original question.
You could screw up your system or weakness its security.
Some users can receive with homebrew permission errors that could be solved without changing the ownership of the whole /usr/ or /usr/local/ directories.
According to my experience, in most cases, it is just enough to restore the ownership for /usr/local/Cellar:
sudo chown -R "$USER" /usr/local/Cellar/
Alternatively, if you have a not standard installation
sudo chown -R "$USER" "$(brew --prefix)/Cellar"
You can try use command ls -l. With this command you will see list of files in current directory with some useful information, like who is owner of this file. So you will see if chown worked out. I believe that chown is a right command for changing ownership.
Exactly this problem has just started occuring on my MacBook.
The owner of /usr/local is:
drwxr-xr-x 24 root wheel 768 Apr 9 17:25 local
I am unable to change the permissions and add myself via Finder - I get a "you don't have the necessary permission" error.
Is this maybe due to security changes in mac os? (I have 10.13.6)

Repairing Postgresql after upgrading to OSX 10.7.3 Lion

I have the issue described in:
Repairing Postgresql after upgrading to OSX 10.7 Lion
but didn't get it solved.
The Postgres application generating the error is at /usr/bin/psql,
but the correct one is at
/Library/PostgreSQL/9.1/bin/psql.
When I add the later to my path in .bash_profile (or even in /etc/paths), uninstall and install the PG gem, then I still receive the error:
.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1161:in `initialize': could not connect to server: Permission denied (PG::Error) Is the server running locally and accepting
I tried it several times, also with the option:
-- --with-pg-config=/Library/PostgreSQL/9.1/bin/pg_config
Any idea?
EDIT:
After reading through more posts (like http://www.iainlbc.com/2011/10/osx-lion-postgres-could-not-connect-to-database-postgres-after-homebrew-installation),
I just did the following:
sudo ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/
And it worked. I don't know at all why. Do I have two installations of Postgres? Where is the app laying behind the /usr/bin/psql file?
After running find / -name .s.PGSQL.5432 -ls, I noticed that the result was:
47366187 0 srwxrwxrwx 1 postgres wheel 0 Dec 3 15:15 /private/tmp/.s.PGSQL.5432
Note that the PGSQL socket file is /private/tmp/.s.PGSQL.5432
Using your suggestion above (but modifying it a bit), I did this:
sudo ln -s /private/tmp /var/pgsql_socket
Restarted terminal and it worked.

Resources