psql: FATAL: Peer authentication failed for user “postgres”

Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Postgresql DBA

psql: FATAL: Peer authentication failed for user “postgres”

Solution 1: Switch to the postgres OS user
sudo -i -u postgres
psql

This works because peer checks if the Linux user is the same as the DB user.

Solution 2: Change authentication method to md5 or scram-sha-256

Edit the pg_hba.conf file (usually in /var/lib/pgsql/<version>/data/ or /home/postgres/data/ if you customized).

Find the line like:

local    all    postgres    peer

Change it to:

local    all    postgres    md5

Save the file and restart PostgreSQL:

sudo systemctl restart postgresql-15

Now set a password for postgres (if not already):

sudo -i -u postgres psql
\password postgres

Then you can connect using:

psql -U postgres -W
Solution 3: Use socket with correct user

If you’re logged in as a different Linux user and don’t want to change pg_hba.conf, connect with:

psql -U postgres -h 127.0.0.1

 

Conclusion

Tuning PostgreSQL for peak performance is a journey, but with a clear understanding of these configuration parameters, you’re well on your way to becoming a pro! At Learnomate Technologies, we’re here to support you every step of the way with top-notch training in PostgreSQL DBA and more.

For more detailed insights and tutorials, do check out our YouTube channelwww.youtube.com/@learnomate, where we regularly share practical tips and deep dives into essential database topics. And if you’re serious about mastering PostgreSQL DBA, head over to our website for our full training program: learnomate.org/training/postgresql-training/.

I’d love to connect with you, so don’t forget to follow my LinkedInhttps://www.linkedin.com/in/ankushthavali/. If you’re eager to read more about various technologies, explore our blog page here: https://learnomate.org/blogs/. Happy learning, and remember—tuning and training go hand-in-hand for database success!