I am trying to run psql
with user postgres
. When I run sudo su - postgres
AND THEN psql
from within the new session, it is working smoothly. In fact, the ~/.bashrc
in that session with use postgres
has the correct PATH
.
However, if I run sudo -u postgres psql
, I get sudo: psql: command not found
. Even though the session where I am running this command (I use the FISH shell) has the correct PATH as well, and I can invoke psql
without the full path with my user.
I need to invoke the command as sudo -u postgres psql
, how can this behavior be explained?
Edit: if (from FISH) I switch to BASH and run sudo -u postgres psql
, it works! I guess it has to do with the FISH path then...
Edit 2: The issue seems to be that the PATH is reset when using sudo
.
➜ ~ psql
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "opc" does not exist
➜ ~ sudo -u postgres psql
sudo: psql: command not found
➜ ~ echo $PATH
/home/opc/.local /home/opc/.local/bin /usr/pgsql-15/bin /usr/pgsql-15/bin /usr/local/bin /usr/bin /usr/local/sbin /usr/sbin
➜ ~ sudo echo $PATH
/home/opc/.local /home/opc/.local/bin /usr/pgsql-15/bin /usr/pgsql-15/bin /usr/local/bin /usr/bin /usr/local/sbin /usr/sbin
➜ ~ sudo -u postgres /usr/pgsql-15/bin/psql
could not change directory to "/home/opc": Permission denied
psql (15.1)
Type "help" for help.
postgres=#
But if I echo $PATH with sudo, it seems fine...