Skip to content

Instantly share code, notes, and snippets.

@neodevelop
Forked from AtulKsol/psql-error-fix.md
Created May 5, 2020 22:10
Show Gist options
  • Select an option

  • Save neodevelop/9084aed9ba93bb36a5a0b0b1f7148e65 to your computer and use it in GitHub Desktop.

Select an option

Save neodevelop/9084aed9ba93bb36a5a0b0b1f7148e65 to your computer and use it in GitHub Desktop.
Solution of psql: FATAL: Peer authentication failed for user “postgres” (or any user)

psql: FATAL: Peer authentication failed for user “postgres” (or any user)

The connection failed because by default psql connects over UNIX sockets using peer authentication, that requires the current UNIX user to have the same user name as psql. So you will have to create the UNIX user postgres and then login as postgres or use sudo -u postgres psql database-name for accessing the database (and psql should not ask for a password).

If you cannot or do not want to create the UNIX user, like if you just want to connect to your database for ad hoc queries, forcing a socket connection using psql --host=localhost --dbname=database-name --username=postgres (as pointed out by @meyerson answer) will solve your immediate problem.

But if you intend to force password authentication over Unix sockets instead of the peer method, try changing the following pg_hba.conf* line:

from

# TYPE DATABASE USER ADDRESS METHOD
local  all      all          peer

to

# TYPE DATABASE USER ADDRESS METHOD
local  all      all          md5
  • peer means it will trust the identity (authenticity) of UNIX user. So not asking for a password.

  • md5 means it will always ask for a password, and validate it after hashing with MD5.

You can, of course, also create more specific rules for a specific database or user, with some users having peer and others requiring passwords.

After changing pg_hba.conf you'll need to restart PostgreSQL if it's running. E.g. sudo service postgresql restart

* The file pg_hba.conf will most likely be at /etc/postgresql/9.x/main/pg_hba.conf

* Source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment