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?

  • Did you really have to enter the password for the root user for sudo su postgres? If yes, how did you set this in the first place?
    – nohillside
    Apr 2, 2013 at 8:45

2 Answers 2

  • 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:


    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.

  • See my comment above; I was getting my Linux and OSX usage confused. Thank you both for clarifying.
    – GChorn
    Apr 2, 2013 at 14:54

I think you are mixing several concepts here which leads to some confusion.

  • sudo required the password of the current user for authorization. So if you run it as yourself, enter your own password. If you run it as postgres (e.g. after having switched to that user), the password of the postgres user would be required. As this is most probably not set (and postgres isn't included in /etc/sudoers either), postgres won't be able run sudo
  • In the way you use it, sudo creates a new shell running with the new user. To switch back to the previous user, just exit the shell (Ctrl-D)
  • Instead of using sudo su <username> it might be easier to skip the su part and just use sudo -su <username> (or just sudo -s if you want to become root)

In practice I found it much easier to open several tabs in Terminal and log into different users in each tab. This way you don't have to worry about switching back and forth all the time.

From sudo(8):

If the invoking user is root or if the target user is the same as the invoking user, no password is required. Otherwise, sudo requires that users authenticate themselves with a password by default (NOTE: in the default configuration this is the user's password, not the root password). Once a user has been authenticated, a time stamp is updated and the user may then use sudo without a password for a short period of time (5 minutes unless overridden in sudoers)

  • sudo default configuration (at least in some Linux flavors) requires that the user types the root password. For example, from /etc/sudoers on SUSE Linux Enterprise Server: In the default (unconfigured) configuration, sudo asks for the root password.
    – jaume
    Apr 2, 2013 at 8:18
  • OS X != Linux :-)
    – nohillside
    Apr 2, 2013 at 8:34
  • I know, seen the man page too, but when I read this in the OP's question: I'm prompted for my root password I thought he meant the password for the root user.
    – jaume
    Apr 2, 2013 at 8:39
  • 1
    Good point, but based on the question I would be surprised if the OP really enabled the root account and changed sudoers accordingly.
    – nohillside
    Apr 2, 2013 at 8:44
  • Completely agree. Edited my answer accordingly.
    – jaume
    Apr 2, 2013 at 8:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .