PostgreSQL Peer Authentication Failed Fix
Install PostgreSQL:
Ubuntu/Debian:
sudo apt update
sudo apt install postgresql postgresql-contrib -y
Check if PostgreSQL is running:
sudo systemctl status postgresql
Start it if needed:
sudo systemctl start postgresql
Enable auto-start:
sudo systemctl enable postgresql
Trying this usually fails:
psql -U postgres
FATAL: Peer authentication failed for user "postgres"
This happens because PostgreSQL uses peer authentication by default on local connections. This means Linux user must match PostgreSQL user, If your Linux session user is ex-introvert and you try connecting as postgres, PostgreSQL rejects the connection.
Temporary solution:
sudo -u postgres psql
This switches to the Linux postgres user before opening PostgreSQL.
Set a password for postgres:
ALTER USER postgres PASSWORD 'yourpassword';
\q
Open PostgreSQL authentication config:
sudo nano /etc/postgresql/[Version_number]/main/pg_hba.conf
[Version_number] may differ, check it manually with < ls /etc/postgresql/ > or use tab for auto completion Find this line:
local all postgres peer
change it to :
local all postgres md5
You may also find:
local all all peer
change it to :
local all all md5
Save the file and Restart PostgreSQL:
sudo systemctl restart postgresql
Now connect using:
psql -U postgres -W
-W forces password authentication.
You should now connect successfully.
Frequently Asked Questions
Why does PostgreSQL use peer authentication by default?+
Peer authentication is designed for local Linux systems where the operating system user is trusted. It avoids password prompts for matching system users but becomes confusing during manual setup or application development.
What does “Peer authentication failed for user postgres” mean?+
It means the current Linux user does not match the PostgreSQL user being requested. PostgreSQL checks the operating system identity before allowing access.
Is md5 authentication better than peer authentication?+
md5 is more practical for development and remote connections because it uses passwords. Peer authentication is mainly useful for tightly controlled local environments.
Why does psql work with sudo -u postgres psql?+
Because the command switches the current Linux session to the postgres system user, matching PostgreSQL's expected peer-authenticated identity.
Where is the pg_hba.conf file located?+
/etc/postgresql/<version>/main/pg_hba.conf The PostgreSQL version directory depends on the installed version.
Why must PostgreSQL be restarted after changing pg_hba.conf?+
Authentication settings are loaded into memory. Restarting or reloading PostgreSQL applies the new authentication configuration.
Continue reading
Oracle Free ARM VM Capacity Error Fix
Automatically create Oracle free VMs when regions are saturated. Use GitHub Actions and scheduled retries to keep attempting instance creation until capacity becomes available.
5 minWhen Your ORM Calls In Sick: Surviving Production With Raw SQL
Remember that 3 AM call? When the ORM folded, and the DBA was unreachable? Yeah. This is about what saves your ass then: raw SQL, from CRUD to the dark magic of indexes and window functions.
5 minUML: Because Sometimes You Need a Map When the Ship's on Fire
We've all been there: staring at logs at 3 AM, wondering why
8 minThat Familiar Ache: Diagnosing the N+1 Query Problem When Your Database Just Can't Anymore
Ever had a simple page grind your database to a halt? The N+1 query problem is often the culprit, a silent killer hiding in plain sight, turning what should be one efficient query into a cascade of costly trips to the database.
12 min