I have similar situation as mentioned here Airflow basic auth - cannot create user
So, I create user like this:
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._set_password = 'set_the_password'.encode('utf8')
session = settings.Session()
session.add(user)
session.commit()
session.close()
And user is created. Kinda. If I exit from python shell and launch it again I can see the user doing
session.query(PasswordUser).all()
or
session.query(models.User).all()
But when I try to login I get "Incorrect login details". I checked all the logs I could grab from airflow and got nothing. Just a request one-liner. Also, I checked postgres db I use. And table "users" is empty for some reason. Though if I delete this table, user creation fails, so I don't even understand in which place this user is saved.
I just had the exact same thing happen... and I just used the correct URI string like so...
import airflow
from airflow import models, settings
from airflow.contrib.auth.backends.password_auth import PasswordUser
user = PasswordUser(models.User())
user.username = 'user'
user.email = '[email protected]'
user.password = 'password'
# the secret sauce is right here
from sqlalchemy import create_engine
engine = create_engine("postgresql://airflow:airflow@postgres:5432/airflow")
session = settings.Session(bind=engine)
session.add(user)
session.commit()
session.close()
exit()
You can also use either PyCharm Pro or pgadmin to connect to the db directly.
Now I've just gotta solve the problem with it still not prompting me to login despite changing the config file.
So I found out that when I run airflow from console, it uses different db than when it is run at webserver. I used this https://github.com/puckel/docker-airflow as the boilerplate to setup my instance in docker. So, if I insert needed script in some dag task and run it from webserver, it fills my postgres db all right and I can then login with a created user. Why the heck different db is used in console - I still don't know, which is different question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With