0
0
AirflowHow-ToBeginner ยท 3 min read

How to Create Connection in Airflow: Step-by-Step Guide

To create a connection in Airflow, use the Airflow UI by navigating to Admin > Connections and adding a new connection with the required details. Alternatively, use the CLI command airflow connections add with appropriate parameters to create connections from the terminal.
๐Ÿ“

Syntax

Airflow connections can be created using the CLI with the following syntax:

  • airflow connections add <CONN_ID> --conn-uri <CONNECTION_URI>: Adds a connection using a full URI.
  • airflow connections add <CONN_ID> --conn-type <TYPE> --conn-host <HOST> --conn-login <LOGIN> --conn-password <PASSWORD> --conn-schema <SCHEMA> --conn-port <PORT>: Adds a connection by specifying individual parts.

Each part defines connection details like host, login, password, and type (e.g., mysql, http, postgres).

bash
airflow connections add <CONN_ID> --conn-uri <CONNECTION_URI>

# OR

airflow connections add <CONN_ID> \
  --conn-type <TYPE> \
  --conn-host <HOST> \
  --conn-login <LOGIN> \
  --conn-password <PASSWORD> \
  --conn-schema <SCHEMA> \
  --conn-port <PORT>
๐Ÿ’ป

Example

This example shows how to create a PostgreSQL connection named my_postgres using the CLI. It demonstrates setting host, login, password, schema, and port.

bash
airflow connections add my_postgres \
  --conn-type postgres \
  --conn-host localhost \
  --conn-login airflow_user \
  --conn-password airflow_pass \
  --conn-schema airflow_db \
  --conn-port 5432
Output
Successfully added connection "my_postgres"
โš ๏ธ

Common Pitfalls

Common mistakes when creating Airflow connections include:

  • Using incorrect connection IDs that do not match usage in DAGs.
  • Forgetting to restart the Airflow scheduler or webserver after adding connections via environment variables.
  • Misformatting the connection URI or missing required fields like conn-type.
  • Not setting the connection in the correct Airflow environment (e.g., different Airflow home or database).

Always verify connection details and test connectivity if possible.

bash
## Wrong: Missing conn-type
airflow connections add my_conn --conn-host localhost

## Right: Include conn-type
airflow connections add my_conn --conn-type mysql --conn-host localhost
๐Ÿ“Š

Quick Reference

MethodDescriptionExample
Airflow UIAdd connection via web interface under Admin > ConnectionsUse UI to fill form and save
CLIAdd connection using airflow CLI commandsairflow connections add my_conn --conn-uri mysql://user:pass@host:3306/db
Environment VariableSet connection via AIRFLOW_CONN_ env varexport AIRFLOW_CONN_MY_CONN='mysql://user:pass@host/db'
โœ…

Key Takeaways

Use Airflow UI or CLI to create connections with required details.
Connection IDs must match those used in your DAGs.
Restart Airflow services if connections are added via environment variables.
Always verify connection details to avoid runtime errors.
Use connection URIs for quick setup or individual parameters for clarity.