我正在使用 docker 運行氣流。
我想在本地主機上的 PostgresSQl 中查詢一些資料。
這是我在我的 dag 中的連接:
def queryPostgresql():
conn_string="dbname='datawarehouse' host='localhost' user='postgres' password='admin'"
conn=db.connect(conn_string)
df=pd.read_sql("select name,city from test",conn)
df.to_csv('data.csv')
print("-------Data Saved------")
我正在添加到氣流的連接:

psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
是否可以從我的 Airflow Docker 在我的 PgSQL 中進行查詢?我應該在 docker 中安裝 PgSQL 嗎?
uj5u.com熱心網友回復:
當您嘗試localhost在 Airflow 中訪問時,它會嘗試連接到在 Airflow 容器上運行的 Postgres,而該容器并不存在。localhost默認情況下,來自容器的不路由到 Docker 主機。
幾個選項:
- 使用
host.docker.internal而不是連接到 Docker 主機localhost - 在 Docker Compose 網路中運行 Airflow 和 Postgres 并通過容器名稱(
psql等)連接
還有一些其他方法與主機檔案等有關,但以上可能是您最簡單的選擇。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/323210.html
