newbie question: postgres: could not change directory

hello!

I’m trying to learn django using this book.
It gives a great low-level tutorial for deploying djang on using cookiecutter.
However when I try to deploy django and connect to my postgres server postgres gives me a permission error:

python manage.py sqlcreate | sudo -u postgres psql -U postgres

results to:

Could not change directory ... permission denied
CREATE ROLE
CREATE DATABASE
GRANT

The error arises on the part after | I’ve found out. My error is therefore a postgres permission error rather than a django error I think.

I’ve found numerous people that are also facing permission errors when using postgres but these solutions have however not helped me understand and fix the error.

I’m running Fedora and systemctl and pg_isready shows everything should be working correctly.

Should I just add the user postgres to my username group to fix this error ? Or does that create a security problem ?

This should be a fairly common error, but can anyone tell me why this is occurring ?

Thank you in advance!

Please see the image below for some more info.

Try running that command in the /var/lib/postgresql directory. That’s the default home directory for the postgres user.
Note, this may also mean that you need to run these two commands separately. Run the first one to direct the output to a file in a location that the postgres user may access, then run the second command reading the file created by the first.

Hi there,
Thank you for the reply.

Unfortunately that folder does not exist, but /var/lib/pgsql does exist. I’ve tried to run the command there but my user is not allowed in that folder. I’ve added postgres user to my username group and vice versa but no lock :frowning:

Become root first then cd into that directory. (e.g. sudo su - )

Hi there,

I should have mentioned that I tried that as well but that it was not successful. I think I should look for a book that also provides a guide on how to use postgres…

[root@fedora everycheese]# postgres psql -U postgres
"root" execution of the PostgreSQL server is not permitted.
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.

Become root, cd to the var/lib/pgsql directory, then run the psql command. (The command doesn’t start with Postgres - that is the parameter to the -U option of the sudo command.)