Home > Enterprise >  How can I a Djanog auth user on the command line and not have to manually enter the password?
How can I a Djanog auth user on the command line and not have to manually enter the password?

Time:12-13

I'm using Django 3.2 and the auth module. I would like to create a super user on the command line (for eventual inclusion in a docker script). When I try this

$ python manage.py createsuperuser --username=joe [email protected]

I'm prompted for a password. The module does not seem to support a "--password" argument ...

$ python manage.py createsuperuser --username=joe [email protected] --password=password
usage: manage.py createsuperuser [-h] [--username USERNAME] [--noinput] [--database DATABASE]
                                 [--email EMAIL] [--version] [-v {0,1,2,3}] [--settings SETTINGS]
                                 [--pythonpath PYTHONPATH] [--traceback] [--no-color] [--force-color]
                                 [--skip-checks]
manage.py createsuperuser: error: unrecognized arguments: --password=password

Is there a way I can auto-create a user without manual intervention?

CodePudding user response:

This is done for security reasons: usually shell command are written to a history file, and if you thus would pass the password as parameter, a hacker could eventually look at the history and thus obtain the password of the superuser. It can read the content of the DJANGO_SUPERUSER_PASSWORD variable to set the password.

You thus can set the password with:

DJANGO_SUPERUSER_PASSWORD=somepassword python manage.py createsuperuser --no-input --username=joe  [email protected]

I would however strongly advise not to set the DJANGO_SUPERUSER_PASSWORD in the script file, but define the environment variable elsewhere.

  • Related