[Django]-Must be owner of relation django_site when manage.py migrate

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

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

1👍

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

GRANT olduser TO newuser;

Leave a comment