2

I am not from a PostgreSQL background and wanted to use it for a project I am currently working on as it was the recommended DB for what I am using.

I use PgAdmin 4 to visualise the database, and my literal understanding of "shut down server" on that application was it shut down the database, however it does not. So I wanted to learn how to start/stop/restart the server at the start of the project so I was better equipped to manage it later down the line.



tl;dr: What is the correct way/command to start a DB, on OSX, not using homebrew, and with it's default settings/directory structure?


The beginning of my trial and errors...

After much reading on many websites I found the way to stop the server, a way which worked for me personally – not using Homebrew and on OSX

Stopping DB

$ sudo -u postgres pg_ctl -D /Library/PostgreSQL/12/data/ stop

could not identify current directory: Permission denied
waiting for server to shut down.... done
server stopped

Super!
The DB stopped. The website wasn't working, as was excepted.

Attempting to start...

Now, naturally trying to start it back up again with

$ sudo -u postgres pg_ctl -D /Library/PostgreSQL/12/data/ start

Yeilds...

could not identify current directory: Permission denied
could not identify current directory: Permission denied
could not identify current directory: Permission denied
The program "postgres" is needed by pg_ctl but was not found in the
same directory as "pg_ctl".
Check your installation.

Uh oh...

So pg_ctl is not in the same directory... perhaps I need to specify it's path in the command

$ sudo -u postgres /Library/PostgreSQL/12/bin/pg_ctl -D /Library/PostgreSQL/12/data/ start
could not identify current directory: Permission denied
could not identify current directory: Permission denied
could not identify current directory: Permission denied
The program "postgres" is needed by pg_ctl but was not found in the
same directory as "pg_ctl".
Check your installation.

Attempt #1

Trying something else, omitting the -D as I might be overcomplicating things...

$ sudo -u postgres pg_ctl start
could not identify current directory: Permission denied
pg_ctl: no database directory specified and environment variable PGDATA unset

PGDATA isn't set... I have it set on my user, so I guess it isn't set for the postgres user. Let try that then

$ sudo -u postgres export PGDATA="/Library/PostgreSQL/12/data/"
sudo: export: command not found

Okay so it's not that. WELL WHAT THEN?!

Reading further I see some using initdb (credit to the final update in this Q) rather than start so I thought I'd give that a go. Nothing else seems to be working so lets try it

$ sudo -u postgres /Library/PostgreSQL/12/bin/pg_ctl -D /Library/PostgreSQL/12/data/ initdb
could not identify current directory: Permission denied
could not identify current directory: Permission denied
could not identify current directory: Permission denied
The program "initdb" is needed by pg_ctl but was not found in the
same directory as "pg_ctl".
Check your installation.

Attempt #2

Maybe rearrange the command a little bit...

$ sudo su postgres pg_ctl initdb -D /Library/PostgreSQL/12/data/
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
/Library/PostgreSQL/12/bin/pg_ctl: /Library/PostgreSQL/12/bin/pg_ctl: cannot execute binary file

Reoccurring theme, what is all this "cannot access directories"? I am running sudo and the postgres user as is everyone else in everything I've read! Why does it not work in my case?..

Attempt #3

$ ls -la /Library/PostgreSQL/12/data/
ls: : Permission denied

Makes sense... I recall trying to access that /data dir before and it was limited to the postgres user.
So let me try doing what I've experimented with so far and attempt ls as postgres

$ sudo -u postgres ls -la /Library/PostgreSQL/12/data/
total 112
[...]

What is this madness?! It works, I have access but I cannot run any of those commands I need to start my DB! sudo has access to all; postgres has access to that dir...

Attempt #4

Lets check some other things.

sudo -u postgres which pg_ctl
/Library/PostgreSQL/12/bin/pg_ctl

So postgres has pg_ctl, and yet cannot run it.

Maybe lets try as my user account, forgetting the postgres user

$ pg_ctl initdb --pgdata=/Library/PostgreSQL/12/data/
The files belonging to this database system will be owned by user "Freemium".
**This user must also own the server process.**

The database cluster will be initialized with locale "en_GB.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.

initdb: error: could not access directory "/Library/PostgreSQL/12/data": Permission denied
pg_ctl: database system initialization failed

So it cannot be my user account and I don't even want to own that process anyway, it's what the postgres user is for.

Attempt #5

Lets try using root – at this point, why not.

$ sudo pg_ctl initdb --pgdata=/Library/PostgreSQL/12/data/
pg_ctl: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.

Nope, even root doesn't want it. In both instances it's hinting that it must be the postgres user. So why tf is it not working???

Attempt #6

At this point I just wondered what would happen if I ran the original stop command

$ sudo -u postgres pg_ctl -D /Library/PostgreSQL/12/data/ stop
could not identify current directory: Permission denied

Hmm... Again with the current directory permissions...

After all these attempts, how does one simply start a PostgreSQL server?



2
  • I don't use a Mac, but I wonder if using an installer like this or this would make things easier
    – user1822
    Commented Aug 27, 2020 at 12:14
  • @a_horse_with_no_name – the enterprisedb installer (your first link) is the one I used. During the installation process I kept the default settings/options that it provides which gave me the directory structure shown throughout the post; so I guess it'll be the same or similar for osx, win, linux but I cannot confirm this so I've only put the osx tag in.
    – Freemium
    Commented Aug 27, 2020 at 14:13

1 Answer 1

1

Answer

The issue is the sudo -u postgres.

Your shell is running as you, but you're running the command as the postgres user. It does not have permission to see the file or even be in the current directory.

[...]

cd to a directory that the postgres user can be in.


So, in order to start the DB you shall need to do the following:

$ cd /Library/PostgreSQL/12/
(or whichever dir your progres lives in)

Then

$ sudo -u postgres pg_ctl -D /Library/PostgreSQL/12/data/ start
waiting for server to start....
done
server started

FINALLY!



The Solution!

As I was writing my question I wanted to make sure I had all bases covered to best convey what I've tried and paint the best picture to everyone who read this. All those messages about root and postgres users not knowing anything about directories was starting to drive me nuts!

How can the magical sudo not handle this?! What does all this mean? What are all these "could not identify current directory: Permission denied" messages anyway?

Somehow, I can't even remember how, but probably on autopilot I searched for "could not identify current directory: Permission denied" as I was running out of things to query regarding this issue and came across this. (all my searches regarding postgres had the search engine know that's specifically what I was looking for, otherwise who knows what else would have came up).

And after a long and painful quest to start the DB I found this gem.

https://stackoverflow.com/a/50993745/2483927

Hmm...

After trying that and running the commands in my answer above I was able to start the DB and everything was working as needed!


I have not been able to find clear and concise information on this issue for days. Most of my search results led me to documentation that didn't help all that much as seen from the attempts in my OP. Others used Homebrew, or other OSes or folder structures that didn't provide too much help; So I hope anyone in a similar situation can find this quickly and easily to avoid all the hassle that I've seen so many go through, including myself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.