How to Fix Peer Authentication Failed Error in PostgreSQL
peer authentication failed error in PostgreSQL happens because the server expects the connecting user to match the system user name. To fix it, change the authentication method in the pg_hba.conf file from peer to md5 or password, then reload PostgreSQL.Why This Happens
PostgreSQL uses different methods to check who can connect. peer authentication means it checks if your operating system user name matches the database user name. If they don't match, you get the error.
This often happens when you try to connect as a database user different from your OS user without proper password authentication.
psql -U postgres # or psql -U someuser
The Fix
Open the pg_hba.conf file, usually located in the PostgreSQL data directory. Find the line that says local all all peer and change peer to md5 to require password authentication.
After saving, reload PostgreSQL with sudo systemctl reload postgresql or equivalent.
Now, when you connect, PostgreSQL will ask for a password instead of relying on OS user matching.
# Original line in pg_hba.conf local all all peer # Change to local all all md5
Prevention
To avoid this error in the future, always ensure your PostgreSQL authentication method matches your connection style:
- Use
md5orpasswordauthentication if you connect with different database users. - Keep your
pg_hba.conffile organized and document changes. - Test connections after changes to confirm authentication works.
Related Errors
Other common PostgreSQL authentication errors include:
- password authentication failed: Happens when the password is wrong or missing.
- FATAL: no pg_hba.conf entry: Means no rule matches your connection attempt.
Fix these by checking passwords and ensuring pg_hba.conf has correct entries.
Key Takeaways
peer to md5 in pg_hba.conf to use password authentication.pg_hba.conf updated to match your connection needs.