Steps to Troubleshoot “django.db.utils.ProgrammingError: permission denied for relation django_migrations”

后端 未结 3 930
闹比i
闹比i 2020-12-08 04:15

What are some basic steps for troubleshooting and narrowing down the cause for the \"django.db.utils.ProgrammingError: permission denied for relation django_migrations\" err

相关标签:
3条回答
  • 2020-12-08 04:35

    As mentioned by @user3062149, this is likely caused by attempting to migrate a database table for which Django's psycopg2 user is not the table owner. For instance, if you have in your project's settings.py

    DATABASES = {
        'default': {
            'USER': 'my_username',
            # ...
    

    You will need to check that the table involved in the Django migration is owned by my_username. To do this in psql, you can use SELECT * FROM pg_tables ORDER BY tableowner;. This uses the view pg_tables, which "provides access to useful information about each table in the database." pg_tables is a part of Postgres' system catalogs, the place where a relational database management system stores schema metadata.

    Say that the table in question is owned by other_username (not my_username).

    To update the owner, you then need to call psql with --username=other_username, then change the owner:

    ALTER TABLE public.<table_name> OWNER TO my_username;
    
    0 讨论(0)
  • 2020-12-08 04:57

    If you receive this error and are using the Heroku hosting platform its quite possible that you are trying to write to a Hobby level database which has a limited number of rows.

    Heroku will allow you to pg:push the database even if you exceed the limits, but it will be read-only so any modifications to content won't be processed and will throw this error.

    0 讨论(0)
  • 2020-12-08 04:59

    I was able to solve my issue based on instructions from this question. Basically, postgres privileges needed to be re-granted to the db user. In my case, that was the user I had setup in the virtual environment settings file. Run the following from the commandline (or within postgres) where mydatabase and dbuser should be your own database and user names:

    psql mydatabase -c "GRANT ALL ON ALL TABLES IN SCHEMA public to dbuser;"
    psql mydatabase -c "GRANT ALL ON ALL SEQUENCES IN SCHEMA public to dbuser;"
    psql mydatabase -c "GRANT ALL ON ALL FUNCTIONS IN SCHEMA public to dbuser;"
    
    0 讨论(0)
提交回复
热议问题