-1

The database detector exists, verified by \l in psql. The user postgres exists verified by SELECT * FROM pg_user;.

#1 test [solved]

I run

sudo -u postgres psql detector -c "SELECT * FROM pg_user"

but get

could not change directory to "/home/masi/det": Permission denied

while getting also the right output of the user table.

Solution in Fabricio's answer and comments i.e. restart shell; previously run the flag --password causes the strange behavior.

#2 test [solved]

Other strange test. I run

sudo -u postgres psql detector --password -h localhost -c 'SELECT * FROM pg_user;'

which gives the following although I am the localhost.

psql: FATAL:  password authentication failed for user "postgres"
FATAL:  password authentication failed for user "postgres"

Solution in Fabricio's answer and comments i.e. restart shell and do not use the flag --password with sudo. Do not use the flag -h localhost with sudo.

Why here is the permission denied error?

Community
  • 1
  • 1
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

1 Answers1

2

You're in /home/masi/det_78_2015 when launching the psql command. Since the user launching the command is postgres, it is not allowed to view the current home directory you are in. This is why the command is successful but it is throwing an error regarding the permissions of the directory.

Does thepostgres user have a home directory in the system?

Fabrizio Mazzoni
  • 1,831
  • 2
  • 24
  • 46