Search code examples
postgresqldockerairflow

Connection refused when calling Postgres container from another docker container


I am using Airflow's Docker Compose to setup Airflow and a Postgres Database in distinct containers. Everything seems fine, but when I try to insert data in Postgres from Airflow I get the following error :

psycopg2.OperationalError: connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused

I don't understand why, since my postgres container seems to run properly on port 5432

$ docker ps -a
CONTAINER ID   IMAGE                   ...   STATUS                   PORTS                                       NAMES
3ba2f9927c21   apache/airflow:2.10.0   ...   Up 5 hours (healthy)     0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   airflow-webserver-1
c02791ed1ccc   apache/airflow:2.10.0   ...   Up 5 hours (healthy)     8080/tcp                                    airflow-scheduler-1
6eceb7c20420   apache/airflow:2.10.0   ...   Up 5 hours (healthy)     8080/tcp                                    airflow-triggerer-1
3bd2fb0e4533   apache/airflow:2.10.0   ...   Up 5 hours (healthy)     8080/tcp                                    airflow-worker-1
1adc814c1cea   apache/airflow:2.10.0   ...   Exited (0) 5 hours ago                                               airflow-init-1
53a8ad18b3ed   postgres:13             ...   Up 5 hours (healthy)     5432/tcp                                    postgres-1
cb3598d5cc4d   redis:7.2-bookworm      ...   Up 5 hours (healthy)     6379/tcp                                    redis-1

When I check the container manually it works fine too :

$ docker exec -it postgres-1 bash
root@53a8ad18b3ed:/# psql -h localhost -U airflow -p 5432
psql (13.16 (Debian 13.16-1.pgdg120+1))
Type "help" for help.

airflow=# \l
                               List of databases
   Name    |  Owner  | Encoding |  Collate   |   Ctype    |  Access privileges  
-----------+---------+----------+------------+------------+---------------------
 airflow   | airflow | UTF8     | en_US.utf8 | en_US.utf8 | 
 postgres  | airflow | UTF8     | en_US.utf8 | en_US.utf8 | 
 template0 | airflow | UTF8     | en_US.utf8 | en_US.utf8 | =c/airflow         +
           |         |          |            |            | airflow=CTc/airflow
 template1 | airflow | UTF8     | en_US.utf8 | en_US.utf8 | =c/airflow         +
           |         |          |            |            | airflow=CTc/airflow
(4 rows)

What am I missing here ?


Solution

  • The error occurs because Airflow is trying to connect to Postgres using localhost, which refers to the Airflow container itself, not the Postgres container.

    Modify the connection string to use the Postgres container name (postgres) instead of localhost.

    In your docker-compose.yml:

    services:
      airflow-webserver:
        environment:
          AIRFLOW__CORE__SQL_ALCHEMY_CONN: postgresql+psycopg2://airflow:airflow@postgres:5432/airflow
    

    Apply the changes:

    docker-compose down
    docker-compose up -d
    

    This should resolve the connection issue.