Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't log into system user postgres

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 - postgres
Password:
su: Sorry

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

like image 426
Incerteza Avatar asked Jun 01 '14 17:06

Incerteza


People also ask

How do I login as postgres user?

There are two ways to login PostgreSQL: By running the "psql" command as a UNIX user which is also configured as PostgreSQL user using so-called IDENT/PEER authentication, e.g., " sudo -u postgres psql ". Via TCP/IP connection using PostgreSQL's own managed username/password (using so-called MD5 authentication).

What is password for default user postgres?

For most systems, the default Postgres user is postgres and a password is not required for authentication. Thus, to add a password, we must first login and connect as the postgres user.

How do I grant superuser permissions to postgres user?

Log into PostgreSQL and run the following ALTER USER command to change user test_user to superuser. Replace test_user with username as per your requirement. postgres-# ALTER USER test_user WITH SUPERUSER; In the above command, we use WITH SUPERUSER clause to change user to superuser.


3 Answers

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

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

See the output of brew 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 at initdb time:

  • a database superuser account with the same login as your OS account (seems to be alex in your case)
  • a database named postgres 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 by brew, 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.

like image 117
Daniel Vérité Avatar answered Oct 21 '22 12:10

Daniel Vérité


You're trying to access the system user named postgres. 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 or postgres_? I know some installers on OS X use weird names. Or perhaps your install never successfully created a postgres user on your system in the first place, so you can't set a password for it?

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

sudo -u postgres psql

or whatever command you need to run as the postgres superuser.

Note that you shouldn't need su for anything.

like image 16
Craig Ringer Avatar answered Oct 21 '22 10:10

Craig Ringer


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

sudo -u postgres -i

Type exit when done. See:

  • PostgreSQL error: Fatal: role "username" does not exist

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

like image 7
Erwin Brandstetter Avatar answered Oct 21 '22 11:10

Erwin Brandstetter