0

I'm trying to log in by passing the host's IP as a parameter, simulating a login from outside:

root@ubuntu-2gb-nbg1-1:/etc/postgresql/12/main# psql -h YYY.YYY.YYY.YYY -p 5433 -d myproject -U myproject
Password for user myproject: 
psql: error: FATAL:  password authentication failed for user "myproject"
FATAL:  password authentication failed for user "myproject"

I then try to log in from the same host with the same password but with different options:

root@ubuntu-2gb-nbg1-1:/etc/postgresql/12/main# psql -h localhost -U myproject -d myproject
Password for user myproject: 
psql (12.6 (Ubuntu 12.6-0ubuntu0.20.04.1), server 10.16 (Ubuntu 10.16-0ubuntu0.18.04.1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

myproject=> 

I am using PostgreSQL 12 and trying to set up a remote login for a service which should write to the DB from the outside. I've already set up the listener in /etc/postgresql/12/main/postgresql.conf to also listen to my remote server and configured my /etc/postgresql/12/main/pg_hba.conf as follows:

# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

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

The problem I have is that when attempting to log in remotely, the password authentication fails. The same username and password is correct when logging in from within the host. It's very strange that the password is being rejected when attempting the remote login. Does anyone have any suggestions?

2
  • 2
    You might be connecting to a different database cluster: the default port is 5432. Commented Apr 7, 2021 at 18:46
  • Thanks for your comment @LaurenzAlbe! It turned out I was running two psql instances without noticing. Commented Apr 14, 2021 at 11:08

1 Answer 1

4

Laurenz Albe's comment actually led me to the solution. It turned out I was running two different instances of PostgreSQL and didn't notice. The one I connected to over localhost was postresql-10 and had the proper user and database and the one I had configured to listen to the outside over port 5433 was postgresql-12 and didn't have the same users and databases.

In the end it was a simple fix to remove one instance and set up everything on the single version 12.

Sign up to request clarification or add additional context in comments.

1 Comment

I wanted to say that this also helped lead me to my solution. In my case, in kubernetes I had the service pointing at the wrong postgresql database container. Thanks this was driving me crazy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.