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
| Method | Description | Example |
|---|---|---|
| Airflow UI | Add connection via web interface under Admin > Connections | Use UI to fill form and save |
| CLI | Add connection using airflow CLI commands | airflow connections add my_conn --conn-uri mysql://user:pass@host:3306/db |
| Environment Variable | Set connection via AIRFLOW_CONN_ | export 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.