Creating a Superuser
Make an admin account with createsuperuser.
Syntax
python manage.py createsuperuserTo log into the admin you need a superuser account. Create one with the createsuperuser command, which prompts for a username, email and password.
Run migrate first so the auth tables exist.
Example
# Ensure auth tables exist
python manage.py migrate
# Create an admin account
python manage.py createsuperuser
# Username: admin
# Email: [email protected]
# Password: ********When to use it
- A developer creates a superuser account immediately after startproject so they can log into the admin during development.
- A DevOps engineer creates a superuser non-interactively in a Docker entrypoint script using environment variables.
- A developer uses the shell to reset a forgotten superuser password without access to the admin login.
More examples
Create superuser interactively
Runs the interactive prompt to create a Django superuser with full admin access.
python manage.py createsuperuser
# Prompts for username, email, and passwordCreate superuser non-interactively
Creates a superuser via environment variables without any interactive prompts, useful in CI/CD.
DJANGO_SUPERUSER_USERNAME=admin \
[email protected] \
DJANGO_SUPERUSER_PASSWORD=securepass123 \
python manage.py createsuperuser --noinputChange password via shell
Resets a user's password programmatically through the Django management shell.
# python manage.py shell
from django.contrib.auth import get_user_model
User = get_user_model()
user = User.objects.get(username="admin")
user.set_password("NewSecurePass!")
user.save()
Discussion