When I try to change user (starting as postgres user) using the command

sudo -i -u ubuntu

The password for postgres is prompted. Now the postgres account was created upon installing postgres, and I don't know what the password is. I have tried changing the password in the psql prompt with

ALTER USER postgres PASSWORD 'password';
ALTER ROLE postgres PASSWORD 'password';
ALTER USER postgres WITH PASSWORD 'password';
ALTER ROLE postgres WITH PASSWORD 'password';
\password ;

These have enabled me to set the password for the database server login, but not for the linux user, which makes sense I guess. And trying it with the passwd command doesn't work because it asks for the old password first. I have added postgres to the /etc/sudoers file as well.


The Unix user postgres and the PostgreSQL user postgres are only related by their names, but otherwise are not the same thing.

To be able to change to another user with sudo from the postgres Unix user account, you will either have to assign the postgres user a password, or allow the user to use sudo in that way without a password (not something I would recommend).

To reset the postgres user password, as root, use

# passwd postgres
  • 1
    @dutchJSCOOP Note too that you should actually need to do any interactive work logged in as the postgres user. This user is only for running the actual PostgreSQL database.
    – Kusalananda
    Sep 23 '17 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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