PostgreSQL “Peer authentication failed”: match the OS user or use passwords
Match your OS user to the database role, or switch pg_hba.conf's local method to scram-sha-256.
Peer auth requires your OS username to equal the database role. Either connect as the matching OS user (sudo -u myuser psql), create a role with your OS name, or change pg_hba.conf's local line to scram-sha-256, set a password with ALTER USER, and reload.
Try these fixes in order
Check your OS username
Peer authentication compares your OS login against the requested role. If they differ, that mismatch is the entire error.
whoamiConnect as the matching OS user
Run psql as the OS user that matches the database role. On Debian/Ubuntu this is often the postgres user for admin work.
sudo -u myuser psqlOr switch the auth method deliberately
If you prefer password login, edit pg_hba.conf (find it with SHOW hba_file) and change the local line's method from peer to scram-sha-256.
Set a password and reload
Set a password for the role and reload the configuration so the new method takes effect.
sudo -u postgres psql -c "ALTER USER myuser WITH PASSWORD 'newpassword';"sudo -u postgres psql -c "SELECT pg_reload_conf();"Interpret what you see
You are on Debian/Ubuntu and never set a password
What it suggests: Debian and Ubuntu default local connections to peer auth: the kernel-reported OS user must equal the requested database role. Passwords are irrelevant here.
Next check: Connect as the matching OS user, e.g. sudo -u myuser psql.
It worked before you added sudo
What it suggests: Running psql under sudo or su changes the OS identity peer auth sees, so a previously working command fails.
Next check: Drop the sudo, or sudo -u to the OS user matching the role.
OS and database usernames cannot match
What it suggests: If your deployment cannot guarantee matching OS and database names, peer is the wrong method and password auth is the deliberate choice.
Next check: Change the local line in pg_hba.conf to scram-sha-256, set a password, and reload.
Why this happens
- pg_hba.conf uses peer authentication for local connections, which requires the OS username to match the database role.
- You switched OS users (for example with sudo or su) and the new OS name has no matching database role.
Still not working?
Do not change the pg_hba.conf method to trust to silence this; it removes authentication for matching local connections entirely. Use scram-sha-256 with a real password, or create a role matching your OS user.
Official sources
These steps are based on the linked documentation and general diagnostics. BugMedic has not independently tested every product and system configuration. Review commands before running them.