I am try to run psql command with postgres role.
(1) sudo psql -U postgres
Since postgres differs from my OS username, I receive the Peer authentication failed for user "postgres" error when I run it.
(2) But when I run sudo -u postgres psql
, it succeeds
I'm not sure how the (2) commands can work because, according to a Postgres document, peer authentication happens automatically locally and my current OS username is different from postgres. (I made no changes to pg_hba.config or pg_ident.config files.)
And what is the difference between the (1) and the (2) command?
CodePudding user response:
In your first attempt, you used sudo
to become the root
user (because there was no -u
option) and tried to connect as database user postgres
, which will fail with peer
authentication because root
is different from postgres
.
In your second attempt, you used sudo
to become user postgres
and called psql
without specifying a username, so that the username defaults to the same as your current operating system user name, namely postgres
. Then peer
authentication works.