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-schemato 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_IDcauses 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-typeor--conn-hostresults 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
| Flag | Description | Example |
|---|---|---|
| --conn-uri | Full connection URI string | postgresql://user:pass@host:5432/dbname |
| --conn-type | Type of connection (e.g., postgres, mysql) | postgres |
| --conn-host | Hostname or IP address | localhost |
| --conn-login | Username for connection | airflow_user |
| --conn-password | Password for connection | airflow_pass |
| --conn-port | Port number | 5432 |
| --conn-schema | Database schema or name | public |
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.