PostgreSQL: FATAL - Peer authentication failed for user (PG::ConnectionBad)

"Peer authentication" means that it's using a unix socket and expecting the connecting unix user to have the same unix username as the postgresql username.

Since your local unix username is funkdified and you're trying to connect as user goodsounds over a unix domain socket (local) connection where your pg_hba.conf specifies peer authentication, Pg correctly rejects your connection attempt.

This is the default behaviour for many installs when using unix sockets.

You can:

  • Connect via TCP/IP by specifying a hostname in your database connection settings;
  • edit pg_hba.conf to use md5 password authentication instead of peer authentication for unix sockets (local connection type) so Pg accepts password authentication; or
  • Connect with a PostgreSQL username the same as your unix username and create the user in PostgreSQL if it doesn't exist yet.

See the docs for pg_hba.conf and the rest of the client authentication chapter of the documentation.

Note that changes to pg_hba.conf do not take effect immediately, you must restart or at least reload PostgreSQL to get it to reread pg_hba.conf.


Oh, also, if you have multiple PostgreSQL versions installed you might have a libpq from one version and a server from another. In this case make sure the location for the unix socket that libpq connects to by default is the same as the server's unix_socket_directories or override it with (e.g.) host=/tmp in your connection string.


I was facing same problem on Ubuntu machine so I removed this error by following some steps. Switch to postgres user

$ sudo su - postgres

it will ask for password and by default password is postgres

After switch the user to postgres, open psql console

$ psql

so check the version of postgres if multiple versions are available

psql=# select VERSION();

PostgreSQL 9.1.13 on x86_64-unk....         # so version is 9.1

Now Open postgres user

vim /etc/postgresql/9.1/main/pg_hba.conf

9.1 is version return form upper command

and replace

local   all             postgres                                peer

to

local   all             postgres                                md5

Restart the service

sudo service postgresql restart

I write steps on my blog also

http://tarungarg402.blogspot.in/2014/10/set-up-postgresql-on-ubuntu.html


If "peer authentication" does not work, try md5 authenticaion.

To specify host try something like this:

 psql -d <dbname> -U <username> -h <hostname>

or this:

 psql -d <dbname> -U <username> -h <hostname> -W