Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting authentication failed error with postgresql from command line

When i try to login from command line like this

psql -h dbserver -U testuser test

then i get this error

psql: FATAL:  Ident authentication failed for user "testuser"

This is my

nano -w /var/lib/pgsql/data/pg_hba.conf

# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD

# "local" is for Unix domain socket connections only
local   all         all                               ident
# IPv4 local connections:
host    all         all         127.0.0.1/32          password
# IPv6 local connections:
host    all         all         ::1/128               ident
host    all     all     192.168.0.1/24  password
like image 542
Mirage Avatar asked Nov 23 '12 14:11

Mirage


People also ask

How do I log into PostgreSQL from terminal?

There are two ways to login PostgreSQL: By running the "psql" command as a UNIX user which is also configured as PostgreSQL user using so-called IDENT/PEER authentication, e.g., " sudo -u postgres psql ". Via TCP/IP connection using PostgreSQL's own managed username/password (using so-called MD5 authentication).

How do I enable postgres authentication?

To authenticate network connections from the PostgreSQL server's machine (non-socket connections) using passwords, you need to match a host connection type instead of local . You can then limit the acceptable addresses to the local loopback devices and allow users to authenticate using md5 or scram-sha-256 .

How do I connect to PostgreSQL shell?

You can also connect to PostgreSQL database using pgAdmin GUI application. Connect to the database at localhost:5432 using the user name postgres and the password supplied. Now, double click on PostgreSQL 9.4 under the "Servers Groups". pgAdmin will ask you for a password.


3 Answers

You can't specify a non-default user with -U in psql when using ident authentication. You won't be running psql as that user, so auth will fail.

You must either use sudo -u testuser psql test or change pg_hba.conf so that authentication (at least for testuser on the db test) uses md5 password authentication, and set a password for testuser with ALTER USER ... PASSWORD if they don't have one already.

like image 66
Craig Ringer Avatar answered Nov 15 '22 09:11

Craig Ringer


I saw the following errors in /var/lib/pgsql/data/pg_log/postgresql-Wed.log:

LOG: provided username (DB_USER) and authenticated username (SHELL_USER) don't match FATAL: Ident authentication failed for user "DB_USER"

The solution in my case was to change all the authentication methods in /var/lib/pgsql/data/pg_hba.conf to md5

like image 24
KalenGi Avatar answered Nov 15 '22 08:11

KalenGi


With abovementioned, there can be two reasons for this issue - testuser not existing - wrong password or authentication failure for the user.

Here is what I did to resolve this in Windows:

  1. Check via pgAdmin: Go to your programs and search for pgAdmin and run. Typically this will be running at http://127.0.0.1:51865/browser/

Check for the Login/Group Roles tab under Server "PostgreSQL". Check if the user is existing. You can also do this by logging to "postgres" user via command "psql -U postgres" and running command "\du"

If user testuser does not exist, create it.

  1. Change the authentication methods in pg_hba.conf to md5. Alternatively,
    change the password for the user with the command: ALTER USER ... PASSWORD.
like image 22
Nandan Chaturvedi Avatar answered Nov 15 '22 08:11

Nandan Chaturvedi