我在Docker上使用气流和Postgres。我有以下连接,其中我做了一个DML命令(外部气流):
# 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()
我正在使用Docker和Postgres的容器在localhost:5433上可用。我可以完美地使用Pgadmin和从Python执行DML命令。
我实例化了Postgress (db
对象)类,并在DAG中创建了以下任务:
insert_into = PythonOperator(
task_id='insert_into',
python_callable=db.insert_into
)
然而,当我打开气流界面这个消息弹出,我不能运行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?
我使用这个Dockerfile的气流。我也试过"连接"。部分,但我得到了相同的信息。我知道了,连接启动并运行。
postgres的主机名是";postgres"不是"localhost".
这个名字被定义为在docker-compose postgres的名称服务。
services:
postgres:
image: postgres:13
还可以在docker中看到连接字符串-compose(粗体)
AIRFLOW__DATABASE__SQL_ALCHEMY_CONN: postgresql + psycopg2://气流:airflow@postgres/气流