I've taken over a system using postgres. When logging in, if I use ...

sudo -u postgres psql postgres

...it does NOT ask for a password. However, if I do ...

psql -U postgres -h localhost postgres

... it asks for the password.

Why? How is this difference configured? Can it be configured for a different user to login this way?

(NOTE: user postgres is NOT in the sudoers file, or in the sudo group.


1 Answer 1


Postgres has a type of authentication called peer. If you look at pg_hba.conf you may see something like

local   all             postgres                       peer

or like

local   all             all                            peer

The fact you did not get asked for the password has nothing to do with using sudo and everything to do with the fact that you were connecting to a DB that matched the user name and peer authentication was enabled in pg_hba.conf.

If this is a test machine and security is not needed you can use trust instead of peer and then passwords will not be needed. Can not stress enough, trust is only a good option for development and test machines. One should never, ever, use trust on a production machine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.