0

I'm trying to log in to a database with this command:

psql -U user my_db

This is the error that gets thrown:

psql: FATAL: Peer authentication failed for user "user"

I figured it had something to do with my pg_hba.conf. I tried several things but couldn't get it to work. This is the relevant part of pg_hba.conf

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   my_db           user                                    md5
local   all             all                                     md5
# IPv4 local connections:
host    my_db           user             127.0.0.1/32           md5
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            ident
host    replication     all             ::1/128                 ident

I also get the same error when I first do su - postgres and the try psql -U user

Edit: Turns out I was modifying the wrong pg_hba.conf. I solved the original problem, now I can log in like this: psql -U user my_db. But my application, which is running on the same device can't access the database with this pg_hba.conf. Checking the logs this is the line that shows up when my application makes new requests: 2019-12-10 16:18:51.144 +03 [29570] LOG: invalid length of startup packet

C.Acarbay
  • 424
  • 5
  • 17
  • Does this answer your question? [Getting error: Peer authentication failed for user "postgres", when trying to get pgsql working with rails](https://stackoverflow.com/questions/18664074/getting-error-peer-authentication-failed-for-user-postgres-when-trying-to-ge) – richyen Dec 10 '19 at 14:14
  • @richyen I tried the same line, it doesn't work – C.Acarbay Dec 10 '19 at 14:19
  • 2
    PostgreSQL doesn't use peer unless you tell it to. The pg_hba.conf you show is not the one which is in effect. Either it is the wrong file, or the server hasn't been restarted since the file was changed. – jjanes Dec 10 '19 at 14:50
  • @jjanes You were right. It was the wrong pg_hba.conf. Now I can access the DB with psql -U user my_db, but my application, which is on the same server, still can't access the db. I'm editing the question to reflect my new pg_hba.conf – C.Acarbay Dec 10 '19 at 15:18
  • Your new pg_hba only shows peer for a replication connection, so I think it is still not a correct representation of the true pg_hba.conf. What OS users does your application server run as? – jjanes Dec 10 '19 at 15:27
  • No it's the right one. It's a fresh setup so I assume the peer method is the default. I solved my other problem it was related to my application's settings. Thanks for the help! – C.Acarbay Dec 10 '19 at 15:51
  • When you say you solved your "other problem," do you still have a problem that needs to be addressed here in Stack Overflow? – richyen Dec 10 '19 at 22:59

0 Answers0