0
0
AirflowHow-ToBeginner ยท 3 min read

How to Create Connection Using CLI in Airflow

Use the airflow connections add command to create a new connection in Airflow via CLI. Specify the connection ID, connection type, and other parameters like host, login, and password in the command.
๐Ÿ“

Syntax

The basic syntax to create a connection using Airflow CLI is:

  • airflow connections add <CONN_ID>: The unique identifier for your connection.
  • --conn-uri <CONN_URI>: The full connection URI string that includes type, login, password, host, port, and schema.
  • Alternatively, use individual flags like --conn-type, --conn-host, --conn-login, --conn-password, --conn-port, and --conn-schema to specify connection details.
bash
airflow connections add <CONN_ID> --conn-uri <CONN_URI>

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

Example

This example creates a PostgreSQL connection named my_postgres with host, login, password, port, and schema specified using individual flags.

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

Common Pitfalls

  • Forgetting to specify a unique CONN_ID causes errors or overwrites existing connections.
  • Using incorrect connection URI format leads to parsing errors.
  • Not restarting Airflow webserver or scheduler after adding connections may cause them not to appear immediately.
  • Missing required parameters like --conn-type or --conn-host results in incomplete connections.
bash
Wrong way (missing conn-type):
airflow connections add my_conn --conn-host localhost

Right way:
airflow connections add my_conn --conn-type http --conn-host localhost
๐Ÿ“Š

Quick Reference

FlagDescriptionExample
--conn-uriFull connection URI stringpostgresql://user:pass@host:5432/dbname
--conn-typeType of connection (e.g., postgres, mysql)postgres
--conn-hostHostname or IP addresslocalhost
--conn-loginUsername for connectionairflow_user
--conn-passwordPassword for connectionairflow_pass
--conn-portPort number5432
--conn-schemaDatabase schema or namepublic
โœ…

Key Takeaways

Use 'airflow connections add' with either --conn-uri or individual flags to create connections via CLI.
Always provide a unique connection ID to avoid conflicts.
Verify connection details carefully to prevent errors.
Restart Airflow services if new connections do not appear immediately.
Use the quick reference table to remember common flags and their purposes.