1

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=joe@example.com

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

$ python manage.py createsuperuser --username=joe --email=joe@example.com --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?

Dave
  • 15,639
  • 133
  • 442
  • 830

1 Answers1

2

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=joe@example.com

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

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555