# Buggy code
def update_index(apps, schema_editor):
    if schema_editor.connection.vendor != "postgresql":
        return
    # This ensures that extensions are loaded into the session. Without that
    # the next ALTER database fails unless we're running as superuser (which
    # is allowed to set non existing parameters, so missing extension doesn't
    # matter)
    # See https://www.postgresql.org/message-id/6376.1533675236%40sss.pgh.pa.us
    schema_editor.execute("SELECT show_limit()")

    schema_editor.execute(
        "ALTER DATABASE {} SET pg_trgm.similarity_threshold = 0.5".format(
            schema_editor.connection.settings_dict["NAME"]
        )
    )


# Traceback (django.db.utils.ProgrammingError)
Operations to perform:
Apply all migrations: accounts, addons, admin, auth, authtoken, checks, contenttypes, fonts, gitexport, lang, memory, screenshots, sessions, sites, social_django, trans, weblate_auth, wladmin
Running migrations:
WARNING Handled exception: ProgrammingError: must be owner of database postgres

Applying memory.0007_use_trigram...Traceback (most recent call last):
File "/opt/weblate/py3-venv/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.InsufficientPrivilege: must be owner of database postgres


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

Traceback (most recent call last):
File "./manage.py", line 32, in <module>
main()
File "/opt/weblate/app/weblate/runner.py", line 32, in main
execute_from_command_line(argv)
File "/opt/weblate/py3-venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/opt/weblate/py3-venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/opt/weblate/py3-venv/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/opt/weblate/py3-venv/lib/python3.7/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/opt/weblate/py3-venv/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/opt/weblate/py3-venv/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 233, in handle
fake_initial=fake_initial,
File "/opt/weblate/py3-venv/lib/python3.7/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/opt/weblate/py3-venv/lib/python3.7/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/opt/weblate/py3-venv/lib/python3.7/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/opt/weblate/py3-venv/lib/python3.7/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/opt/weblate/py3-venv/lib/python3.7/site-packages/django/db/migrations/operations/special.py", line 190, in database_forwards
self.code(from_state.apps, schema_editor)
File "/opt/weblate/app/weblate/memory/migrations/0007_use_trigram.py", line 18, in create_index
schema_editor.connection.settings_dict["NAME"]
File "/opt/weblate/py3-venv/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 142, in execute
cursor.execute(sql, params)
File "/opt/weblate/py3-venv/lib/python3.7/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/opt/weblate/py3-venv/lib/python3.7/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/opt/weblate/py3-venv/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/opt/weblate/py3-venv/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/opt/weblate/py3-venv/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: must be owner of database postgres

# Fixed code
def update_index(apps, schema_editor):
    if schema_editor.connection.vendor != "postgresql":
        return
    # This ensures that extensions are loaded into the session. Without that
    # the next ALTER database fails unless we're running as superuser (which
    # is allowed to set non existing parameters, so missing extension doesn't
    # matter)
    # See https://www.postgresql.org/message-id/6376.1533675236%40sss.pgh.pa.us
    schema_editor.execute("SELECT show_limit()")

    schema_editor.execute(
        "ALTER ROLE {} SET pg_trgm.similarity_threshold = 0.5".format(
            schema_editor.connection.settings_dict["USER"]
        )
    )