Search code examples
djangopostgresqlazure

connect to PostgreSQL server: FATAL: no pg_hba.conf entry for host "" with external database azure


I have a django docker container running. And I try to connect with the azure postgres db.

But when I log in in the admin panel from django, I get this error:

OperationalError at /admin/login/
FATAL:  password authentication failed for user "welzijn"
FATAL:  no pg_hba.conf entry for host "0.0.145.0", user "welzijn", database "welzijn", no encryption

And I thought that I had the solution. Because a lot of resources suggest that you have to edit the pg_hba file and the postgresql file. So I did that:

pgb_hba:

host  all  all 0.0.0.0/0 scram-sha-256

And postgresql:

listen_addresses = '*'

I rebooted the machine. But it seems that it didn't solve the issue. I still get this error:


OperationalError at /admin/login/
FATAL:  password authentication failed for user "welzijn"
FATAL:  no pg_hba.conf entry for host "0.0.145.0", user "welzijn", database "welzijn", no encryption
Request Method: POST
Request URL:    http://localhost/admin/login/?next=/admin/
Django Version: 4.2.4
Exception Type: OperationalError
Exception Value:    
FATAL:  password authentication failed for user "welzijn"
FATAL:  no pg_hba.conf entry for host "85.148.145.211", user "welzijn", database "welzijn", no encryption
Exception Location: /py/lib/python3.9/site-packages/psycopg2/__init__.py, line 122, in connect
Raised during:  django.contrib.admin.sites.login
Python Executable:  /py/bin/python
Python Version: 3.9.9
Python Path:    
['/usr/src/app',
 '/py/bin',
 '/usr/local/lib/python39.zip',
 '/usr/local/lib/python3.9',
 '/usr/local/lib/python3.9/lib-dynload',
 '/py/lib/python3.9/site-packages']
Server time:    Wed, 23 Aug 2023 11:47:25 +0200

Second thing I tried was in azure the setting require_secure_transport turned off Also, not solved the issue.

So I am really stuck on this issue

This is the .env.prod file I am using in the docker-compose-deploy.yml file:

DEBUG=0
SECRET_KEY="+)d)1o(&r3+24)x#i^bk8p8r)@jl_q2=%usxw="
DB_NAME="welzijn"
DB_USER="welzijn"
DB_PASSWORD="password"
DB_HOST="db-wellzijn-nvwa.postgres.database.azure.com"
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1] 0.0.0.0

And this is my docker-compose-deploy.yml file:

version: "3.9"

services:
  web:
    build:
      context: .
      dockerfile: Dockerfile.prod
    restart: always

    command: gunicorn Welzijn.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - static-data:/vol/web
    expose:
      - 8000
    environment:
      - "POSTGRES_HOST_AUTH_METHOD=trust"
    env_file:
      - ./.env.prod

  proxy:
    build: ./proxy
    restart: always
    depends_on:
      - web
    ports:
      - 1337:80
    volumes:
      - static-data:/vol/staticfiles

volumes:
  static-data:

Question: how to resolve the issue: FATAL: no pg_hba.conf entry for host ...


Solution

  • Under the authentication for the database you might have allowed Azure active directory authentication only and that could be the reason that the standalone users (Postgres authentication) are not working.