psql: fe_sendauth: no password supplied
sudo
does not retain most environment variables. If you want to specify environment variables to a command run under sudo
, do so though sudo
:
sudo PGPASSWORD="postgres" -u postgres psql -d pg_ldap -w --no-password -h localhost -p 5432 -t -c "SELECT id FROM radusers WHERE id=1"
Whether or not sudo
permits this will depend on the security policy in force on your site.
I don't recommend this approach as it exposes the password in the command-line history and in the process list. It's much better to use a .pgpass
file, or preferably set pg_hba.conf
up for peer
authentication of local
connections from user postgres
.
You can use a .pgpass
file, but it must be the .pgpass
of the user you're sudo
'ing to, not the user you're sudo
ing from; it'd need to be ~postgres/.pgpass
in this case. Think about it: psql
running as postgres
doesn't know you ran it via sudo
from your account, it doesn't know what your user account is, and even if it did it doesn't have read permission as user postgres
to ~youruser/.pgpass
.
Additionally, -w
is the same as --no-password
. There's no point specifying both.