Zoran Pandovski
By Zoran Pandovski

Psycopg Peer authentication failed for user "postgres"

Psycopg Peer authentication failed for user

psycopg2.OperationalError: FATAL: Peer authentication failed for user “postgres”

The Psycopg is one of the most popular and widely used python database adapters for the PostgreSQL database.

The basic example shows how easy it is to connect to the existing database with psycopg. But, it doesn’t provide all of the

required arguments to the connect function that usually leads to the above error. The basic module usage example:

1
2
3
4
5
6
7
import psycopg2

# Connect to an existing database

conn = psycopg2.connect("dbname=test user=postgres")

Since the psycopg2.OperationalError error is quite generic to avoid it make sure you are providing the following arguments:

  • user - the database user used to authenticate

  • password - the password for the database user

  • database - the database name

  • host - the database host address

The basic example with the required arguments:

1
2
3
conn = psycopg2.connect(dbname='db', user='postgres', host='localhost', password='postgres')

Note: the connection parameters can be added as a connection string or as a set of keyword arguments.