1

The Postgres installers kindly provided to the community by EnterpriseDB.com creates a postgres user account on macOS as is common.

In a console session in Terminal.app, I need to switch to that user. My goal is to run a text editor with that user’s privileges so that I can edit the pg_hba.conf file controlling Postgres user-authentication rules.

When I try:

su - postgres

…I am prompted for the user's password, which I enter successfully, yet get the error message:

su: Sorry

I am sure the password is correct. What can I do to access the postgres user?

1

The EBD installer asks for a password during the installation but it is meant for the database user, not the postgres Unix user, which doesn't have a password.

This is mentioned in Postgres, Passwords and Installers on EDB site. Quote:

On Unix-like operating systems such as Linux and Mac OS X, the account is setup without a password and users generally never need to worry about it again.

The most plausible explanation to su - postgres not working is for you is that you're confusing the database password with the OS password.

To make su - postgres usable, the OS password would need to be set first, which could be done with:

$ sudo passwd postgres

This is also true for PostgreSQL packaged in Linux distributions. In fact, the PostgreSQL packages for Linux even go one step further and don't ask a password for the database superuser, which allows for a fully automated installation. Just like the OS password is not necessary if sudo is always used, a database password is not necessary if only the default peer authentication method is ever used by the postgres user.

1

sudo su postgres

Having found this closed Question on Stack Overflow and its duplicate, I will post here the solutions found there and there.

➥ You must use the sudo command combined with the su command.

sudo su postgres

When prompted, enter your macOS admin user password rather than the postgres user’s password. At this juncture, your Mac admin user is the one executing the sudo command, not the postgres user.

While I do not understand why we must combine the sudo with the su, it does indeed work for me in macOS Mojave. I did not find this tip among my various BSD and Linux related searching efforts, so perhaps this is a Mac-only Apple thing, perhaps an extra security measure. Or perhaps in the Unix-style OSes, the su command must be run by the root user, and in macOS the root account is disabled by default for security purposes – our Mac admin user account is less powerful than root, and may not be authorized by default to run su? Dunno, don't care, the workaround works.

Modern alternative: sudo -u

According to Craig Ringer and to Erwin Brandstetter, the more modern form is:

sudo -u postgres -i

According to this page:

  • -u means “run a specified command as this specified user”.
  • postgres is specifying the username under which we want to run the command.
  • -i simulates an initial login, meaning the shell will be initialized running that user’s startup scripts.
  • No command is provided, so an interactive shell is executed.

For more discussion on the older and newer approaches, see this Question on sister site, What's the difference between sudo su - postgres and sudo -u postgres?. The answer there by Craig Ringer advises: (a) Forget about sudo su, and (b) use sudo -u instead.

Run apps as postgres user

To run a psql session as the postgres user:

sudo -u postgres psql

To run the nano text-editor app as the postgres user:

sudo -u postgres nano

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.