pythonpostgresqldockerdocker-composesqlalchemy

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: no pg_hba.conf entry for host, didnt change anything


I am working on an old Flask sqlalchemy website which is run with docker-compose, started giving me this error. I have tried changing the hostname in the app config, and going back to an old version from gitlab, but im not sure what else i can do.

here are all the relevant pieces of code i think that might be useful for you:

INIT.PY
from flask import Flask
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_sqlalchemy import SQLAlchemy
from flask_mail import Mail
import os


app = Flask(__name__)

app.config["SECRET_KEY"] = "FLASK_SECRET_KEY"
app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://flask:flask@db:5432/flask_dev"

app.config["MAIL_SERVER"] = "smtp.gmail.com"
app.config["MAIL_PORT"] = 587
app.config["MAIL_USE_TLS"] = True
app.config["MAIL_USERNAME"] = os.environ.get("MAIL_USERNAME")
app.config["MAIL_PASSWORD"] = os.environ.get("MAIL_PASSWORD")

db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
login_manager = LoginManager(app)
mail = Mail(app)

from website import routes
APP.PY
from website import db, app

if __name__ == "__main__":
    db.create_all()
    app.run(debug=True, host="0.0.0.0")
DOCKER-COMPOSE.YAML
version: "3.3"

services:

  db:
    environment:
      - POSTGRES_USER=flask
      - POSTGRES_PASSWORD=flask
      - POSTGRES_DB=flask_dev
    image: postgres:latest
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:  
      - "5400:5432"
    networks:
      - backend
    restart: 
      always
    
  web:
    build: .
    ports:
      - "5000:5000"
    env_file: .env
    depends_on:
      - db
    networks:
      - backend
    restart: 
      always



volumes:
  postgres_data:

networks:
  backend:
DOCKERFILE
FROM python

WORKDIR /app

COPY requirements.txt /app/

RUN pip install --requirement requirements.txt

COPY . /app/

CMD ["python","app.py"]

FULL ERROR TRACEBACK

ubuntu@ubuntu-admin:~/Desktop/home-website-master/hawala-system$ docker-compose up 
[+] Running 3/3
 ⠿ Network hawala-system_backend  Created                                                                                                                     1.1s
 ⠿ Container hawala-system-db-1   Created                                                                                                                     3.7s
 ⠿ Container hawala-system-web-1  Created                                                                                                                     3.4s
Attaching to hawala-system-db-1, hawala-system-web-1
hawala-system-db-1   | 
hawala-system-db-1   | PostgreSQL Database directory appears to contain a database; Skipping initialization
hawala-system-db-1   | 
hawala-system-db-1   | 2022-08-19 11:44:36.587 UTC [1] LOG:  starting PostgreSQL 14.5 (Debian 14.5-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
hawala-system-db-1   | 2022-08-19 11:44:36.587 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
hawala-system-db-1   | 2022-08-19 11:44:36.588 UTC [1] LOG:  listening on IPv6 address "::", port 5432
hawala-system-db-1   | 2022-08-19 11:44:38.419 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
hawala-system-db-1   | 2022-08-19 11:44:39.121 UTC [26] LOG:  database system was shut down at 2022-08-19 11:44:20 UTC
hawala-system-db-1   | 2022-08-19 11:44:40.493 UTC [1] LOG:  database system is ready to accept connections
hawala-system-web-1  | /usr/local/lib/python3.10/site-packages/flask_sqlalchemy/__init__.py:872: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
hawala-system-web-1  |   warnings.warn(FSADeprecationWarning(
hawala-system-db-1   | 2022-08-19 11:44:41.765 UTC [33] FATAL:  no pg_hba.conf entry for host "172.24.0.3", user "flask", database "flask_dev", no encryption
hawala-system-web-1  | Traceback (most recent call last):
hawala-system-web-1  |   File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 3361, in _wrap_pool_connect
**EXTRA TRACEBACK REMOVED**
hawala-system-web-1  |   File "/usr/local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
hawala-system-web-1  |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
hawala-system-web-1  | psycopg2.OperationalError: FATAL:  no pg_hba.conf entry for host "172.24.0.3", user "flask", database "flask_dev", no encryption
hawala-system-web-1  | 
hawala-system-web-1  | 
hawala-system-web-1  | The above exception was the direct cause of the following exception:
hawala-system-web-1  | 
hawala-system-web-1  | Traceback (most recent call last):
hawala-system-web-1  |   File "/app/app.py", line 5, in <module>
hawala-system-web-1  |     db.create_all()
*unuseful
hawala-system-web-1  |   File "/usr/local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
hawala-system-web-1  |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
hawala-system-web-1  | sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL:  no pg_hba.conf entry for host "172.24.0.3", user "flask", database "flask_dev", no encryption
hawala-system-web-1  | 
hawala-system-web-1  | (Background on this error at: https://sqlalche.me/e/14/e3q8)

Solution

  • I had an old volume running, I removed all of them, then I added a healthcheck to my db service, and I made the web service depend on the db service to confirm that the service was healthy before attaching it self; heres the updated code.

    version: "3.3"
    
    services:
    
      web:
        build: .
        ports:
          - "5000:5000"
        env_file: .env
        depends_on:
          db:
            condition: service_healthy
        networks:
          - backend
        restart: 
          always
    
      db:
        environment:
          - POSTGRES_USER=flask
          - POSTGRES_PASSWORD=flask
          - POSTGRES_DB=flask_dev
        image: postgres:latest
        volumes:
          - postgres_data:/var/lib/postgresql/data
        ports:  
          - "5400:5432"
        networks:
          - backend
        healthcheck:
            test: [ "CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}" ]
        restart: 
          always
    
    volumes:
      postgres_data:
    
    networks:
      backend: