Password problems changing users in Terminal

passwordpostgresqlsudoterminal

I'd like to switch between the default user in Terminal and the user 'postgres', created when I installed PostgreSQL 9.2.

If I type su postgres, I'm prompted for a password that I don't know and don't remember ever setting. I try the root password and it doesn't work.

If I type sudo su postgres, I'm prompted for my root password, which I enter, and that works.

But now, if I try to switch back to the default user using sudo su user, I'm prompted for a password, and the root password does not work. Neither does the password I use to log into that user in OSX.

I don't understand what's going on here–I've only ever set one password, but the only context in which it seems to work is when switching from the normal user to 'postgres'. What's going on and how can I fix it?

Best Answer

  • To switch to user postgres, type in Terminal:

    sudo su - postgres
    

    If you are asked for a password, type yours.

    (The - in sudo su - changes the environment to that of the new user, and it's probably what you want. See man su for details.)

  • Now, to switch back to your user, simply type:

    exit
    

    to exit the current bash session and go back to your original bash session.

Note that if you type sudo su <user> as user postgres you need to identify yourself to sudo and type postgres's password, but as you say, it hasn't any, so it won't work. It may even be the case that user postgres is not allowed to use sudo at all.