+1 vote
in Databases by (17.2k points)

When I try to access PostgreSQL database using psql command, I get the following error:

"psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user"

How can I fix it?

1 Answer

+2 votes
by (365k points)
selected by
 
Best answer

"Peer authentication failed" means PostgreSQL is using peer authentication, which checks whether the current Linux system user matches a PostgreSQL role with the same name. For example, if you’re logged in as the Linux user john, PostgreSQL expects a database role named john. If they don’t match, you’ll see this error.

To fix the issue, do the following:

  • Open the PostgreSQL authentication configuration file: /etc/postgresql/17/main/pg_hba.conf. (Replace 17 with your actual PostgreSQL version if different.)
  • Change this line:
local all all ident 

to 

local all all md5

  • Save the file and restart PostgreSQL: 
sudo systemctl restart postgresql
After this change, PostgreSQL will use password-based authentication (md5) instead of matching Linux usernames, and the "peer authentication failed" error should be resolved.

Related questions


...