我正在使用Airflow 的 Docker Compose在不同的容器中设置 Airflow 和 Postgres 数据库。一切似乎都很好,但是当我尝试从 Airflow 向 Postgres 插入数据时,出现以下错误:
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
我不明白为什么,因为我的 postgres 容器似乎在端口 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
当我手动检查容器时它也能正常工作:
$ 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)
我在这里遗漏了什么?
发生该错误的原因是 Airflow 尝试使用 连接到 Postgres
localhost
,它指的是 Airflow 容器本身,而不是 Postgres 容器。修改连接字符串以使用 Postgres 容器名称(
postgres
)而不是localhost
。在您的
docker-compose.yml
:应用更改:
这应该可以解决连接问题。