14

I installed postgresql on mac using brew. I wasn't asked about for the password for postgresql during the installation. Now I need to create a user and can't:

Alexs-MacBook-Air:mydir alex$ su - postgresPassword:su: Sorry

Whatever password (including empty) I use, it is wrong. What's the easiest way to reset it?

Erwin Brandstetter's user avatar
Erwin Brandstetter
669k160 gold badges1.2k silver badges1.3k bronze badges
askedJun 1, 2014 at 17:02
Incerteza's user avatar
2

5 Answers5

39

I installed postgresql on mac using brew. I wasn't asked about for the password for postgresql during the installation.

That's normal becausebrew doesn't need or create anypostgres account.The PostgreSQL processes run under your own account. The other 3 answers so far are wrong in this regard.

See the output ofbrew info postgres for information.

To create a new database account, you may launch, from your own OS account:

/usr/local/bin/createdb someusername

or within psql:

/usr/local/bin/psql -d postgres

and then as an SQL command:CREATE USER someusername PASSWORD 'somepassword';

This should work because brew has normally created atinitdb time:

  • a database superuser account with the same login as your OS account (seems to bealex in your case)
  • a database namedpostgres that may be used to log info for administrative tasks such as creating a user.

The point of using the full path/usr/local/bin is to reach the commands provided bybrew, as opposed to the commands with the same name that come with the system and are located in/usr/bin or the commands with the same name that are potentially installed by other PostgreSQL providers, such as postgres.app or macports or entreprisedb. There are 5-6 competing and incompatible ways of getting postgresql installed on Mac OS X.

EDIT: the newer versions of MacOS X desktop edition no longer have the postgres client-side commands pre-installed. This seems to be the case at least since MacOS X 10.10 (Yosemite) and possibly 10.9.

answeredJun 2, 2014 at 12:56
Daniel Vérité's user avatar
Sign up to request clarification or add additional context in comments.

3 Comments

This should've been the accepted answer. Brew's installation doesn't have apostgres user.
Is there any way to password protect this system level user? When I runpsql postgres and then\password it has no effect.
@bryankennedy: most probably this is due topg_hba.conf usingpeer ortrust locally. Replace these withmd5.
16

You're trying to access thesystem user namedpostgres. According to comments you left on the other answers, there is no such user. You can't change the password of a user that doesn't exist.

Perhaps it's_postgres orpostgres_? I know some installers on OS X use weird names. Or perhaps your install never successfully created apostgres user on your system in the first place, so you can't set a password for it?

In general younever need to set a password for this user. You can just

sudo -u postgres psql

or whatever command you need to run as thepostgres superuser.

Note thatyou shouldn't needsu for anything.

answeredJun 2, 2014 at 6:40
Craig Ringer's user avatar

Comments

7

There is no default password. To run a shell as userpostgres use (as advised by Craig):

sudo -u postgres -i

Typeexit when done. See:

Turns out, the user wasn't created at all. Look to@Daniel's answer.

answeredJun 1, 2014 at 18:02
Erwin Brandstetter's user avatar

5 Comments

actually it says "su: unknown login: postgres"
Why does everyone usesudo su - postgres?sudo supports user-impersonation directly, and without the horrible quoting mess you land up withsu. Justsudo -u postgres psql or whatever.
@CraigRinger: If I plan do do a couple of things as thepostgres user, it's much more convenient to switch to the role andexit when done. And what "quoting mess"?
@ErwinBrandstetter For a login shell usesudo -u postgres -i. As for quoting:su passes its argument to$SHELL -c. So you'll have undesired results from (say)sudo su - postgres -c psql -c "SELECT 1;", wherassudo -u postgres psql -c "SELECT 1;" works correctly. To do the equivalent withsu requires another layer of quoting to protect the argument, likesudo su - postgres -c "psql -c \"select 1\"";. Usingsu as well assudo is just redundant. Finally, if you have asudoers that lets you becomepostgres but notroot, it just won't work.
Thanks, I learned something. I was usingsudo su out of habbit.sudo -u postgres -i seems like the better alternative.
4

Why not:

sudo passwd postgres

This will prompt you for the password of your current account. Then it will ask you to enter the new password (twice) for thepostgres user.

answeredJun 1, 2014 at 18:06
Tamer Shlash's user avatar

1 Comment

actually it says "su: unknown login: postgres"
0

I'm Nura, after I installed postgresql with homebrew on macOS Catalina MBP 13 inch Mid 2012,i do got the same error as Mr. Incerteza. I SOLVED it with different ways, when I got the error, I ran

  1. brew services start postgresql@14
  2. /usr/local/bin/psql -d postgres

Then the terminal database has open.

answeredMay 26, 2023 at 18:31
Muhammad Irfansyahfalah's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.