Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

password authentication failed for user "postgres" on mac

I have problem creating new psql user because I cannot log in psql as "postgres", I have tried

1. sudo -u postgres psql

2. sudo -u postgres createuser img_site -P -s -e

and they are all ask for password of "postgres" which I don't know. I have tried to change unix password of user "postgres"(I know it's dangerous) and it still tells me: password authentication failed for user "postgres". I also have tried GUI pgAdmin but it's the same error.

I don't know if it's related: I have created a symbolic link

sudo ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/

in order to get rid of error

createuser: could not connect to database postgres: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
like image 927
ycshao Avatar asked Sep 01 '13 04:09

ycshao


People also ask

How do I enable password authentication in PostgreSQL?

PostgreSQL database passwords are separate from operating system user passwords. The password for each database user is stored in the pg_authid system catalog. Passwords can be managed with the SQL commands CREATE ROLE and ALTER ROLE, e.g., CREATE ROLE foo WITH LOGIN PASSWORD 'secret' , or the psql command \password .

What is default password for 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.

Does postgres work on Mac?

PostgreSQL can also be installed on macOS using Homebrew. Please see the Homebrew documentation for information on how to install packages. A list of PostgreSQL packages can be found using the Braumeister search tool.


1 Answers

Check pg_hba.conf. it should have a line like this at the top (before all other entries):

local   all             postgres                                peer

This allows local Unix-domain socket access by postgres db user to all databases with no password required.

Now clear and redefine the password for postgres system user (which is automatically created during PostgreSQL installation):

sudo passwd -d postrges
sudo su postgres -c passed

The special thing about this user account is that postgres server allows it to connect to the database, no questions asked.

Now, to define an explicit password for postgres db user using which you can login via other means than local Unix-domain socket connection, run:

su postgres -c psql template1
psql> ALTER USER postgres WITH PASSWORD '<password>';

You will be asked for the postgres system user account password before this command can be run. On successful completion, type \q to quit psql shell, and you are done with resetting the password for postgres db user.

like image 198
zaadeh Avatar answered Sep 19 '22 19:09

zaadeh