Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(auth): give a helpful error for createsuperuser (#61298)

Phil Haack1d8d94afb6dd
diff --git a/posthog/models/test/test_user_model.py b/posthog/models/test/test_user_model.py
index 49dbfc14980f..14d6160509ea 100644
--- a/posthog/models/test/test_user_model.py
+++ b/posthog/models/test/test_user_model.py
@@ -2,6 +2,9 @@ import datetime
from posthog.test.base import BaseTest
+from django.core.management import call_command
+from django.core.management.base import CommandError
+
from posthog.constants import AvailableFeature
from posthog.models import Team, User
from posthog.models.organization import Organization, OrganizationMembership
@@ -16,6 +19,18 @@ class TestUser(BaseTest):
self.assertNotEqual(user.distinct_id, "")
self.assertNotEqual(user.distinct_id, None)
+ def test_create_superuser_raises_with_guidance(self):
+ with self.assertRaises(CommandError) as ctx:
+ User.objects.create_superuser(email="[email protected]", password="12345678")
+ self.assertIn("doesn't support `createsuperuser`", str(ctx.exception))
+ self.assertIn("generate_demo_data", str(ctx.exception))
+ self.assertIn("is_staff", str(ctx.exception))
+
+ def test_createsuperuser_command_fails_with_guidance(self):
+ with self.assertRaises(CommandError) as ctx:
+ call_command("createsuperuser", "--noinput", "--email", "[email protected]")
+ self.assertIn("doesn't support `createsuperuser`", str(ctx.exception))
+
def test_analytics_metadata(self):
self.maxDiff = None
# One org, one team, anonymized
diff --git a/posthog/models/user.py b/posthog/models/user.py
index 592035ccb568..c335fe74583c 100644
--- a/posthog/models/user.py
+++ b/posthog/models/user.py
@@ -1,8 +1,9 @@
from collections.abc import Callable
from functools import cached_property
-from typing import TYPE_CHECKING, Any, Optional, TypedDict, cast
+from typing import TYPE_CHECKING, Any, NoReturn, Optional, TypedDict, cast
from django.contrib.auth.models import AbstractUser, BaseUserManager
+from django.core.management.base import CommandError
from django.db import models, transaction
from django.utils.translation import gettext_lazy as _
@@ -115,6 +116,22 @@ class UserManager(BaseUserManager):
user.save()
return user
+ def create_superuser(self, *args: Any, **kwargs: Any) -> NoReturn:
+ # PostHog has no superuser concept — the `is_superuser` field was removed in 2020 (PR #2026)
+ # and `User.is_superuser` is now a read-only alias for `is_staff`. Django's `createsuperuser`
+ # command calls this method, so we shadow it here to fail with guidance instead of a confusing
+ # `AttributeError`. (We can't override the command itself: `django.contrib.auth` is listed
+ # before `posthog` in INSTALLED_APPS, so its `createsuperuser` always wins resolution.)
+ raise CommandError(
+ "PostHog doesn't support `createsuperuser`. There's no superuser concept here — "
+ "`is_superuser` is a read-only alias for `is_staff`.\n\n"
+ "To set up a local admin instead:\n\n"
+ " • Demo environment: python manage.py generate_demo_data\n"
+ " • Existing account: sign up in the web app, then grant staff access:\n"
+ ' python manage.py shell -c "from posthog.models import User; '
+ "u = User.objects.get(email='[email protected]'); u.is_staff = True; u.save()\""
+ )
+
def bootstrap(
self,
organization_name: str,
diff --git a/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_from_query.ambr b/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_from_query.ambr
index 800a3fff4b5e..4d81dc6a2021 100644
--- a/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_from_query.ambr
+++ b/posthog/session_recordings/queries/test/listing_recordings/__snapshots__/test_session_recording_list_from_query.ambr
@@ -1122,7 +1122,7 @@
WHERE and(equals(s.team_id, 99999), greaterOrEquals(s.min_first_timestamp, toDateTime64('2020-12-29 00:00:00.000000', 6, 'UTC')), lessOrEquals(s.min_first_timestamp, toDateTime64('today', 6, 'UTC')), globalIn(s.session_id,
(SELECT nullIf(nullIf(events.`$session_id`, ''), 'null') AS session_id
FROM events
- WHERE and(equals(events.team_id, 99999), notEmpty(nullIf(nullIf(events.`$session_id`, ''), 'null')), lessOrEquals(events.timestamp, now64(6, 'UTC')), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.person_properties, 'email'), ''), 'null'), '^"|"$', ''), 'bla'), 0), greaterOrEquals(events.timestamp, toDateTime64('2020-12-28 00:00:00.000000', 6, 'UTC')))
+ WHERE and(equals(events.team_id, 99999), notEmpty(nullIf(nullIf(events.`$session_id`, ''), 'null')), lessOrEquals(events.timestamp, now64(6, 'UTC')), equals(events.mat_pp_email, 'bla'), greaterOrEquals(events.timestamp, toDateTime64('2020-12-28 00:00:00.000000', 6, 'UTC')))
GROUP BY nullIf(nullIf(events.`$session_id`, ''), 'null')
HAVING 1
ORDER BY min(toTimeZone(events.timestamp, 'UTC')) DESC