[Fixed]-Django create superuser from batch file


As it seems you can’t provide a password with the echo ''stuff | cmd, the only way I see to do it is to create it in Python:

python manage.py syncdb --noinput
echo "from django.contrib.auth.models import User; User.objects.create_superuser('admin', 'admin@example.com', 'pass')" | python manage.py shell
python manage.py runserver


As of Django 3.0 (per the docs) you can use the createsuperuser --no-input option and set the password with the DJANGO_SUPERUSER_PASSWORD environment variable, e.g.,

DJANGO_SUPERUSER_PASSWORD=my_password ./manage.py createsuperuser \
    --no-input \
    --username=my_user \

or, using all environment variables:

DJANGO_SUPERUSER_EMAIL=my_user@domain.com \
./manage.py createsuperuser \


And if as is good practice you are using a custom user (CustomUser in this case)

echo "from django.contrib.auth import get_user_model; CustomUser = get_user_model();  CustomUser.objects.create_superuser('me', 'nt@example.co.uk', 'mypwd')" | python manage.py shell
👤Nick T

Leave a comment