Home > Enterprise >  Apache Airflow can't connect to localhost database
Apache Airflow can't connect to localhost database

Time:07-13

I am using Airflow and Postgres on Docker. I have the following connection where I do a DML command (outside Airflow):

 # Constructor method initializing the connection to DB and creating cursor
    def __init__(self, db="projeto-pos", user="postgres", host="localhost", password=my_psswd, port="5433"):
        self.conn = psycopg2.connect(
            database=db, host=host, port=port, password=password, user=user)
        self.cur = self.conn.cursor()

I am using Docker and the Postgres' container is available on localhost:5433. I can perfectly use Pgadmin and do DML commands from Python.

I instantiated the Postgress (db ojbect) class and created the following task in a DAG:

insert_into = PythonOperator(
        task_id='insert_into',
        python_callable=db.insert_into
    )

However, when I open the Airflow UI this messages pops up and I can't run the DAG:

Broken DAG: [/opt/airflow/dags/projeto_api.py] Traceback (most recent call last):
  File "/opt/airflow/dags/functions/db_manipulation.py", line 10, in __init__
    database=db, host=host, port=port, password=password, user=user)
  File "/home/airflow/.local/lib/python3.7/site-packages/psycopg2/__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "localhost" (127.0.0.1), port 5433 failed: Connection refused
    Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (::1), port 5433 failed: Cannot assign requested address
    Is the server running on that host and accepting TCP/IP connections?

I am using this Dockerfile for Airflow. I also tried in the "connections" section in the Airflow UI, but I got the same message. I know for sure that the connection is up and running.

CodePudding user response:

The hostname of postgres is "postgres" not "localhost".

this name is defined by the name of the postgres service in the docker-compose.

services:
  postgres:
    image: postgres:13

also, you can see the connection string in the docker-compose (in bold)

AIRFLOW__DATABASE__SQL_ALCHEMY_CONN: postgresql psycopg2://airflow:airflow@postgres/airflow

  • Related