Anatol
Anatol

Reputation: 2033

"error retrieving current directory" running psql commands under sudo

from a bash script I´m trying to create a postgresql database + users. It looks like this:

#!/usr/bin/env bash
echo ' Create DBs'
sudo -u postgres createdb -E 'utf-8' -l en_US.utf8 -T template0 testdb
echo ' Create User'
sudo -u postgres  psql -c "create role bert with login password 'pass';"
echo ' alter permissions'
sudo -u postgres  psql -c "alter database testdb owner to bert;"

unfortuantely I´m getting following errors:

==> default: Create DBs
==> default: could not identify current directory: No such file or directory
==> default: Create User
==> default: shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
==> default: CREATE ROLE
==> default: alter permissions
==> default: shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory

I would be very thankful if one coule explain what´s going wrong and how to work around the getcwd error.

thanks!

Upvotes: 1

Views: 3292

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295363

Your script is being run from somewhere (such as /root -- or possibly a previously-deleted temporary directory) where the postgres user does not have access to the current working directory.

These messages are effectively harmless (unless you're invoking scripts that depend on $PWD or similar variables or commands), but if you want to avoid them, change directories to somewhere certain to exist and which the postgres user has access to. For instance, put the line:

cd /

...at the top of your script, just under the shebang.

Upvotes: 3

Related Questions