BackendをPostgreSQLにしてairflow standaloneでAirflowを動かす手順
このエントリは、backendをPostgreSQLにして、airflow standalone
でairflowを動かす手順のメモです。
何も指定せず起動するとBackendはsqliteになります。
Quick Start | Apache Airflow
https://airflow.apache.org/docs/apache-airflow/stable/start.html
試した環境は、WSL上のUbuntu22.04です。
PostgreSQLのインストール
PostgreSQLのインストール。
sudo apt install postgresql postgresql-contrib
PostgreSQLの有効化、起動。
sudo systemctl enable postgresql
sudo systemctl start postgresql
データベースの作成
Airflow用のデータベースを作成。
dropdb --if-exists airflow_db
createdb airflow_db
Airflow用のDBユーザの作成。
sudo su - postgres -c psql
> CREATE USER airflow_user WITH PASSWORD 'airflow_pass';
> GRANT ALL PRIVILEGES ON DATABASE airflow_db TO airflow_user;
> exit;
データベースへの接続確認。
psql -h localhost airflow_db airflow_user
Airflowアプリの作業ディレクトリ準備
作業ディレクトリ・仮想環境を準備。
mkdir airflow-posgtresql-study && cd $_
python -m venv venv
. venv/bin/activate
以下の手順を参考に、Airflowをインストール。
Quick Start | Apache Airflow
https://airflow.apache.org/docs/apache-airflow/2.10.4/start.html
AIRFLOW_VERSION=2.10.4
# Extract the version of Python you have installed. If you're currently using a Python version that is not supported by Airflow, you may want to set this manually.
# See above for supported versions.
PYTHON_VERSION="$(python -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')"
CONSTRAINT_URL="https://raw.githubusercontent.com/apache/airflow/constraints-${AIRFLOW_VERSION}/constraints-${PYTHON_VERSION}.txt"
# For example this would install 2.10.4 with python 3.8: https://raw.githubusercontent.com/apache/airflow/constraints-2.10.4/constraints-3.8.txt
pip install "apache-airflow[celery]==${AIRFLOW_VERSION}" --constraint "${CONSTRAINT_URL}"
psycopg2もインストール。
pip install psycopg2
環境変数の設定
環境変数はdirenvを使って設定します。
direnv
https://github.com/direnv/direnv
.envrc
export AIRFLOW_HOME=$PWD/.data/airflow
export AIRFLOW__CORE__EXECUTOR=CeleryExecutor
export AIRFLOW__CORE__DAGS_FOLDER=$PWD/dags
export AIRFLOW__CORE__LOAD_EXAMPLES=False
export AIRFLOW__DATABASE__SQL_ALCHEMY_CONN=postgresql+psycopg2://airflow_user:airflow_pass@localhost:5432/airflow_db
# Backendをsqlite以外にしたので、CeleryExecutorを利用できます。
Airflowの起動
Airflowを起動。
airflow standalone
以上。