27

After long long search about postgre, about manage.py killorphant, about django_site, nothing was able to help me with this error:

Synchronizing apps without migrations:
  Creating tables...
  Installing custom SQL...
  Installing indexes...
Running migrations:
  Applying sites.0002_auto_20150929_1444...Traceback (most recent call last):
  **File "/var/www/webapps/lib/python3.4/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: must be owner of relation django_site**

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

Traceback (most recent call last):
  File "./manage.py", line 11, in <module>
    execute_from_command_line(sys.argv)
  File "/var/www/webapps/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/var/www/webapps/lib/python3.4/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/var/www/webapps/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/var/www/webapps/lib/python3.4/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/var/www/webapps/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 161, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/var/www/webapps/lib/python3.4/site-packages/django/db/migrations/executor.py", line 68, in migrate
    self.apply_migration(migration, fake=fake)
  File "/var/www/webapps/lib/python3.4/site-packages/django/db/migrations/executor.py", line 102, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/var/www/webapps/lib/python3.4/site-packages/django/db/migrations/migration.py", line 108, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/var/www/webapps/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 139, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/var/www/webapps/lib/python3.4/site-packages/django/db/backends/schema.py", line 470, in alter_field
    self._alter_field(model, old_field, new_field, old_type, new_type, old_db_params, new_db_params, strict)
  File "/var/www/webapps/lib/python3.4/site-packages/django/db/backends/schema.py", line 642, in _alter_field
    self.execute(self._create_unique_sql(model, [new_field.column]))
  File "/var/www/webapps/lib/python3.4/site-packages/django/db/backends/schema.py", line 111, in execute
    cursor.execute(sql, params)
  File "/var/www/webapps/lib/python3.4/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/var/www/webapps/lib/python3.4/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/var/www/webapps/lib/python3.4/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/var/www/webapps/lib/python3.4/site-packages/django/utils/six.py", line 658, in reraise
    raise value.with_traceback(tb)
  File "/var/www/webapps/lib/python3.4/site-packages/django/db/backends/utils.py", line 65, in execute
    **return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: must be owner of relation django_site**

So, after checking and re checking, django-site is in the installed apps list, site_id = 1 ; my database works except for some images problems.. Well then I tried to do some migrations but this error keep on running and I am now desperate on finding the issue.

Does someone has a clue about it ?

markwalker_
  • 12,078
  • 7
  • 62
  • 99
Jay Cee
  • 1,855
  • 5
  • 28
  • 48
  • See answer: https://stackoverflow.com/questions/1348126/postgresql-modify-owner-on-all-tables-simultaneously-in-postgresql – PhoebeB Jun 23 '21 at 19:31

3 Answers3

46

The database user you use for django to connect to the database is not the owner of the table. You need to change it on the postgres shell or maybe pgadmin3 can help.

Something like:

ALTER DATABASE your_db OWNER TO your_django_db_user
ALTER TABLE django_site OWNER TO your_django_db_user
djangonaut
  • 7,233
  • 5
  • 37
  • 52
11

In my case, I want to change all table ownership to the db_user so I did the below script to change all table ownership to the db_user.

Open your shell

sudo -s -u postgres

for multiple database:

export user="db_user"
export dbs="db1 db2"

then

for db in $dbs; do
    psql -c "alter database $db owner to $user" $db;
done

for db in $dbs; do
    psql -c "alter schema public owner to $user" $db;
done

for db in $dbs; do
    tables=`psql -qAt -c "select tablename from pg_tables where schemaname = 'public';" $db`

    for tbl in $tables; do
        psql -c "alter table \"$tbl\" owner to $user" $db;
    done;
done

for db in $dbs; do
    seqs=`psql -qAt -c "select sequence_name from information_schema.sequences where sequence_schema = 'public';" $db`

    for seq in $seqs; do
        psql -c "alter table \"$seq\" owner to $user" $db ;
    done;
done

for db in $dbs; do
    views=`psql -qAt -c "select table_name from information_schema.views where table_schema = 'public';" $db`

    for view in $views; do
        psql -c "alter table \"$view\" owner to $user" $db ;
    done;
done

Then migrate

python manage.py migrate

source link

Akash D
  • 780
  • 2
  • 11
  • 27
1

None of the other answers worked for me, finally this worked as postgres user in psql shell:

GRANT olduser TO newuser;

Jens Timmerman
  • 9,316
  • 1
  • 42
  • 48