# Airflow Hook

> Code that logs in to one system for you, like a database. Tasks use it.

**Field:** Data Engineering  
**Category:** Orchestration  
**Also known as:** Hook (Airflow), PostgresHook, S3Hook

## Think of it like

It is like a phone that already knows how to dial. You say who to call, and it does the rest.

## Definition

An Airflow hook is ready-made code for talking to one outside system. There is one for PostgreSQL, one for Amazon S3 and many more. You give it a connection name, and it logs in for you. Then you call its methods to run a query or move a file. Many operators are built on top of a hook. You use a hook directly when no operator does quite what you need.

## Example

Lucy needs a query that no ready-made task offers. In her task she makes a PostgresHook with the shop_db connection. She runs the query in two lines.

## Example code

```python
from airflow.providers.postgres.hooks.postgres import PostgresHook

hook = PostgresHook(postgres_conn_id="shop_db")
rows = hook.get_records("SELECT COUNT(*) FROM orders")
```

The hook logs in with the shop_db connection and runs one query.

## Why it matters

It saves you from writing login and retry code for every system. The hook already knows how that system works.

## Common mistake

Making a hook at the top of the DAG file. That code runs each time the file is read, not just when the task runs.

## Three principles

- **One system each.** A hook per database or service.
- **Uses a connection.** Logs in by conn_id.
- **Under operators.** Operators are built from hooks.

## Related terms

- [Airflow Connection](https://learn.benibix.com/term/data/airflow-connection)
- [Airflow Operator](https://learn.benibix.com/term/data/airflow-operator)
- [Airflow Provider Package](https://learn.benibix.com/term/data/airflow-provider-package)
- [Apache Airflow](https://learn.benibix.com/term/data/apache-airflow)
- [PostgreSQL](https://learn.benibix.com/term/development/postgresql)

## Told apart from

- [Airflow Hook vs Operator](https://learn.benibix.com/compare/airflow-hook-vs-airflow-operator): People think a hook is just another kind of operator. A hook is a helper that logs in to one system and talks to it. It is not a task and never shows in the graph. An operator is a task. Many operators use a hook inside to do their work. When no operator fits, you call the hook from your own task.

![A connection box named shop_db holds the host, login and password outside the code. An arrow leads into a dashed box, the provider package apache-airflow-providers-postgres, which holds a highlighted hook that talks to Postgres and an operator, a ready-made task. An arrow leads on to the shop database outside Airflow.](https://learn.benibix.com/diagrams/dat2-airflow-integrations.svg)

---

Source: https://learn.benibix.com/term/data/airflow-hook
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
