Postgres Not Allowing Localhost But Works with 127.0.0.1

Postgres not allowing localhost but works with 127.0.0.1

In pg_hba.conf, the first match counts. The manual:

The first record with a matching connection type, client address,
requested database, and user name is used to perform authentication.
There is no "fall-through" or "backup": if one record is chosen and
the authentication fails, subsequent records are not considered. If no
record matches, access is denied.

Note the reversed order:

host    all         all         127.0.0.1/32          trust
host all all 127.0.0.1/32 ident

But:

host    all         all        localhost             ident
host all all localhost trust

Remember to reload after saving changes to pg_hba.conf. (Restart is not necessary.) The manual:

The pg_hba.conf file is read on start-up and when the main server
process receives a SIGHUP signal. If you edit the file on an active
system, you will need to signal the postmaster (using pg_ctl reload,
calling the SQL function pg_reload_conf(), or using kill -HUP) to
make it re-read the file.

If you really "add" the lines like you wrote, there should not be any effect at all. But if you replace the lines, there is.

In the first case, you get trust authentication method, which is an open-door policy. The manual:

PostgreSQL assumes that anyone who can connect to the server is
authorized to access the database with whatever database user name
they specify (even superuser names)

But in the second case you get the ident authentication method, which has to be set up properly to work.

Plus, as Cas pointed out later, localhost covers both IPv4 and IPv6, while 127.0.0.1/32 only applies to IPv4.

If you are actually using the outdated version 8.4, go to the old manual for 8.4. You are aware that 8.4 has reached EOL in 2014 and is not supported any more? Consider upgrading to a current version.

In Postgres 9.1 or later you would rather use peer than ident.

More:

  • Run batch file with psql command without password

Can't connect to Postgresql via 127.0.0.1

Not sure what happened but a hard reboot of my laptop fixed the issue.

Django + Docker: connection to server at localhost (127.0.0.1), port 5432 failed

The postgres database is no longer running at localhost. In your case (since you named the container db) it is db.

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'lk_potok_2',
'USER': 'postgres',
'PASSWORD': 'post222',
'HOST': 'db',
'PORT': 5432,
},

I don't really see why you would add this in here:

environment:
- DATABASE_URL=postgres://postgres:post222@localhost:5432/lk_potok_2"

since you don't use it in your settings.py. But here it wil also have to be db instead of localhost.

--EDIT--

Explanation as why docker can recognise the other containers can be found here.



Related Topics



Leave a reply



Submit