How to Set Timezone in Apache Airflow Correctly
To set the timezone in Airflow, update the
default_timezone setting in the airflow.cfg file to your desired timezone (e.g., UTC or America/New_York). Alternatively, set the environment variable AIRFLOW__CORE__DEFAULT_TIMEZONE to override the config file.Syntax
Airflow uses the default_timezone configuration under the [core] section in airflow.cfg to set the timezone. You can specify timezones using IANA timezone names like UTC or America/New_York.
Alternatively, you can set the environment variable AIRFLOW__CORE__DEFAULT_TIMEZONE to the timezone string to override the config file.
ini
[core] default_timezone = America/New_York
Example
This example shows how to set the timezone to UTC in airflow.cfg and verify it in a DAG.
python
[core] default_timezone = UTC # Example DAG snippet to print current execution date in configured timezone from airflow import DAG from airflow.operators.python import PythonOperator from datetime import datetime import pendulum def print_execution_date(**kwargs): print(f"Execution date: {kwargs['execution_date']}") default_args = { 'start_date': pendulum.datetime(2024, 1, 1, tz='UTC') } dag = DAG('timezone_example', default_args=default_args, schedule_interval='@daily') print_task = PythonOperator( task_id='print_execution_date', python_callable=print_execution_date, dag=dag )
Output
Execution date: 2024-01-01 00:00:00+00:00
Common Pitfalls
- Not restarting Airflow services after changing
airflow.cfgso the new timezone is not applied. - Using invalid timezone strings that are not recognized by the
pendulumlibrary Airflow uses. - Forgetting that DAG
start_dateand other datetime objects should use timezone-aware datetime to avoid confusion. - Setting timezone only in DAG code but not in Airflow config, which can cause inconsistent scheduling.
ini
[core] default_timezone = Invalid/Timezone # Wrong # Correct [core] default_timezone = UTC
Quick Reference
Summary tips for setting timezone in Airflow:
- Set
default_timezoneinairflow.cfgunder[core]. - Use valid IANA timezone names like
UTC,Europe/London, orAmerica/New_York. - Restart Airflow scheduler and webserver after changes.
- Use timezone-aware datetime objects in DAGs.
- Override config with environment variable
AIRFLOW__CORE__DEFAULT_TIMEZONEif needed.
Key Takeaways
Set the timezone in airflow.cfg under the [core] section with default_timezone.
Use valid IANA timezone names like UTC or America/New_York.
Restart Airflow services after changing timezone settings.
Use timezone-aware datetime objects in DAG definitions.
You can override config with AIRFLOW__CORE__DEFAULT_TIMEZONE environment variable.