Django test database creation fails: permission denied to create extension vector

poetry run python manage.py test minigames.test.models

response in terminal:

The currently activated Python version 3.12.9 is not supported by the project (^3.13).
Trying to find and use a compatible version.
Using python3.13 (3.13.3)
Found 4 test(s).
Creating test database for alias 'default'...
Got an error creating the test database: database "test_aivector_db" already exists
Type 'yes' if you would like to try deleting the test database 'test_aivector_db', or 'no' to cancel:

This is a consistent issue I am facing I have already assigned superuser permission to this db, and also some times it never happens (mostly it resolves or stopes asking after I change the port (from 5432 to 5431)) but when i type yes it gives error that super user permission required to create vector extension (it is already created in db) but as i understand Django test creates a test db separately I don't think a superuser test db is possible but what I understand from all the previous attempts that this is issue with not deleting the previous test db or I maybe completely wrong.

here is the error I get if I type 'yes':

Creating test database for alias 'default'...
Got an error creating the test database: database "test_aivector_db" already exists
Type 'yes' if you would like to try deleting the test database 'test_aivector_db', or 'no' to cancel: yes
Destroying old test database for alias 'default'...
Traceback (most recent call last):
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/db/backends/utils.py", line 103, in _execute
    return self.cursor.execute(sql)
           ~~~~~~~~~~~~~~~~~~~^^^^^
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/psycopg/cursor.py", line 97, in execute
    raise ex.with_traceback(None)
psycopg.errors.InsufficientPrivilege: permission denied to create extension "vector"
HINT:  Must be superuser to create this extension.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/path/to/project/manage.py", line 23, in <module>
    main()
    ~~~~^^
  File "/path/to/project/manage.py", line 19, in main
    execute_from_command_line(sys.argv)
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
    ~~~~~~~~~~~~~~~^^
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/core/management/commands/test.py", line 24, in run_from_argv
    super().run_from_argv(argv)
    ~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/core/management/base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/core/management/base.py", line 459, in execute
    output = self.handle(*args, **options)
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/core/management/commands/test.py", line 63, in handle
    failures = test_runner.run_tests(test_labels)
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/test/runner.py", line 1066, in run_tests
    old_config = self.setup_databases(
        aliases=databases,
        serialized_aliases=suite.serialized_aliases,
    )
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/test/runner.py", line 964, in setup_databases
    return _setup_databases(
        self.verbosity,
    ...<5 lines>...
        **kwargs,
    )
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/test/utils.py", line 206, in setup_databases
    connection.creation.create_test_db(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        verbosity=verbosity,
        ^^^^^^^^^^^^^^^^^^^^
    ...<2 lines>...
        serialize=serialize_alias,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^    
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/db/backends/base/creation.py", line 78, in create_test_db
    call_command(
    ~~~~~~~~~~~~^
        "migrate",
        ^^^^^^^^^^
    ...<3 lines>...
        run_syncdb=True,
        ^^^^^^^^^^^^^^^^
    )
    ^    
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/core/management/__init__.py", line 194, in call_command
    return command.execute(*args, **defaults)
           ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/core/management/base.py", line 459, in execute
    output = self.handle(*args, **options)
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/core/management/base.py", line 107, in wrapper
    res = handle_func(*args, **kwargs)
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/core/management/commands/migrate.py", line 357, in handle
    post_migrate_state = executor.migrate(
        targets,
    ...<3 lines>...
        fake_initial=fake_initial,
    )
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/db/migrations/executor.py", line 135, in migrate
    state = self._migrate_all_forwards(
        state, plan, full_plan, fake=fake, fake_initial=fake_initial
    )
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards
    state = self.apply_migration(
        state, migration, fake=fake, fake_initial=fake_initial
    )
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/db/migrations/executor.py", line 255, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/db/migrations/migration.py", line 132, in apply
    operation.database_forwards(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        self.app_label, schema_editor, old_state, project_state
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^    
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/contrib/postgres/operations.py", line 28, in database_forwards
    schema_editor.execute(
    ~~~~~~~~~~~~~~~~~~~~~^
        "CREATE EXTENSION IF NOT EXISTS %s"
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        % schema_editor.quote_name(self.name)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^    
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/db/backends/postgresql/schema.py", line 48, in execute
    return super().execute(sql, None)
           ~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/db/backends/base/schema.py", line 202, in execute
    cursor.execute(sql, params)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/sentry_sdk/utils.py", line 1860, in runner
    return sentry_patched_function(*args, **kwargs)
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/sentry_sdk/integrations/django/__init__.py", line 653, in execute
    result = real_execute(self, sql, params)
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/db/backends/utils.py", line 79, in execute
    return self._execute_with_wrappers(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        sql, params, many=False, executor=self._execute
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^    
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/db/backends/utils.py", line 92, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/db/backends/utils.py", line 100, in _execute
    with self.db.wrap_database_errors:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/django/db/backends/utils.py", line 103, in _execute
    return self.cursor.execute(sql)
           ~~~~~~~~~~~~~~~~~~~^^^^^
  File "/path/to/project/.cache/pypoetry/virtualenvs/configuration--0DV9FaY-py3.13/lib/python3.13/site-packages/psycopg/cursor.py", line 97, in execute
    raise ex.with_traceback(None)
django.db.utils.ProgrammingError: permission denied to create extension "vector"
HINT:  Must be superuser to create this extension.

in addition my db settings are :


DATABASE_URL=postgres://minidb:postgres@localhost:5432/aivector_db

---
# settings.py database settings
DATABASES = {
    'default': dj_database_url.config(conn_health_checks=True, engine='dj_db_conn_pool.backends.postgresql'),
}

Note: nope just change ports does not make any difference, last time it worked somehow but this time it is not working. I think I am missing.

well solved this issue by giving BYPASSRLS and CREATDB . I don't know which one solved.

one more thing i want to add is that while trying to resolve it i managed to get rid of above error and got connection error I think the the changing of role resolved the connection issue but i still fully don't understand what resolved the vector problem because db already had superuser role.

Вернуться на верх