Airflow user creation

If anyone is looking a way that is easy to understand then here's the thing.

Activate you airflow python environment and start python shell. Then with the help of these commands you can easily set a new user in airflow.

>>> import airflow
>>> from airflow import models, settings
>>> from airflow.contrib.auth.backends.password_auth import PasswordUser
>>> user = PasswordUser(models.User())
>>> user.username = 'new_user_name'
>>> user.email = '[email protected]'
>>> user.password = 'set_the_password'
>>> session = settings.Session()
>>> session.add(user)
>>> session.commit()
>>> session.close()
>>> exit()

If you want to create a admin user you can simply do that by adding user.superuser = True It'll do the job.


The user models in Airflow are currently simplistic and (as of at least 1.9.0) there is no way via the UI to set passwords.

The approach I use is the following python script:

#!/usr/bin/env python

import argparse
import getpass
import sys


def create_user(opts):
    from airflow.contrib.auth.backends.password_auth import PasswordUser
    from airflow import models, settings

    u = PasswordUser(models.User())
    u.username = opts['username']
    u.email = opts['email']
    u.password = opts['password']

    s = settings.Session()
    s.add(u)
    s.commit()
    s.close()

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('email')
    parser.add_argument('username', nargs='?', help="Defaults to local part of email")
    args = parser.parse_args()

    if not args.username:
        # Default username is the local part of the email address
        args.username = args.email[:args.email.index('@')]

    args.password = getpass.getpass(prompt="Enter new user password: ")
    confirm = getpass.getpass(prompt="Confirm:  ")

    if args.password != confirm:
        sys.stderr.write("Passwords don't match\n")
        sys.exit(1)
    create_user(vars(args))

This versoin doesn't support changing passwords as we haven't needed it yet


As of Airflow 1.10, there is an airflow create_user CLI: https://airflow.apache.org/cli.html#create_user.

It supports roles and passwords:

airflow create_user [-h] [-r ROLE] [-u USERNAME] [-e EMAIL] [-f FIRSTNAME]
                    [-l LASTNAME] [-p PASSWORD] [--use_random_password]

Update: As of Airflow 2, this has been rolled into airflow users create: https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#create_repeat1

airflow users create [-h] -e EMAIL -f FIRSTNAME -l LASTNAME [-p PASSWORD] -r
                     ROLE [--use-random-password] -u USERNAME