Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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?

share|improve this question
    
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? –  patrix Apr 2 '13 at 8:45
add comment

2 Answers

up vote 1 down vote accepted
  • 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.

share|improve this answer
    
See my comment above; I was getting my Linux and OSX usage confused. Thank you both for clarifying. –  GChorn Apr 2 '13 at 14:54
add comment

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)

share|improve this answer
    
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 '13 at 8:18
    
OS X != Linux :-) –  patrix Apr 2 '13 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 '13 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. –  patrix Apr 2 '13 at 8:44
    
Completely agree. Edited my answer accordingly. –  jaume Apr 2 '13 at 8:49
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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