psycopg2.errors.InsufficientPrivilege:关系 django_migrations 的权限被拒绝

Posted

技术标签:

【中文标题】psycopg2.errors.InsufficientPrivilege:关系 django_migrations 的权限被拒绝【英文标题】:psycopg2.errors.InsufficientPrivilege: permission denied for relation django_migrations 【发布时间】:2020-10-09 19:58:20 【问题描述】:

我的 DB 的 settings.py 是什么样子的:

ALLOWED_HOSTS = ['*']

DATABASES = 
    'default': 
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'fishercoder',
        'USER': 'fishercoderuser',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '5432',
    

我以这种方式创建了一个名为“fishercoder”的新空数据库:

psql -U postgres
create database fishercoder; 
ALTER USER postgres with password 'badpassword!'; 
CREATE USER fishercoderuser WITH PASSWORD 'password';
ALTER ROLE fishercoderuser SET client_encoding TO 'utf8';
ALTER ROLE fishercoderuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE fishercoderuser SET timezone TO 'PST8PDT';
GRANT ALL PRIVILEGES ON DATABASE fishercoder TO fishercoderuser;

然后我通过运行:psql -U postgres fishercoder < fishercoder_dump.sql 成功地将我的其他 SQL 转储导入到这个新数据库中:

然后我尝试在此 EC2 实例上的 Django 项目上运行 ./manage.py makemigrations,但收到此错误:

Traceback (most recent call last):
  File "/home/ubuntu/myprojectdir/myprojectenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.InsufficientPrivilege: permission denied for relation django_migrations

我在 SO 上找到了这三个相关的帖子: One、two 和 three

我尝试了他们建议的命令:

postgres=# GRANT ALL ON ALL TABLES IN SCHEMA public to fishercoderuser;
GRANT
postgres=# GRANT ALL ON ALL SEQUENCES IN SCHEMA public to fishercoderuser;
GRANT
postgres=# GRANT ALL ON ALL FUNCTIONS IN SCHEMA public to fishercoderuser;
GRANT

不走运,然后我重新启动了我的 postgresql 数据库:sudo service postgresql restart 当我再次尝试运行迁移时,仍然面临同样的错误。

更多调试信息如下:

ubuntu@ip-xxx-xxx-xx-xx:~$ psql -U postgres
Password for user postgres:
psql (10.12 (Ubuntu 10.12-0ubuntu0.18.04.1))
Type "help" for help.

postgres=# \dt django_migrations
Did not find any relation named "django_migrations".
postgres=# \d django_migrations
Did not find any relation named "django_migrations".
postgres=#  \dp django_migrations
                            Access privileges
 Schema | Name | Type | Access privileges | Column privileges | Policies
--------+------+------+-------------------+-------------------+----------
(0 rows)

postgres=# SHOW search_path; \dt *.django_migrations
   search_path
-----------------
 "$user", public
(1 row)

Did not find any relation named "*.django_migrations".

postgres=# \dn+ public.
                                     List of schemas
        Name        |  Owner   |  Access privileges   |           Description
--------------------+----------+----------------------+----------------------------------
 information_schema | postgres | postgres=UC/postgres+|
                    |          | =U/postgres          |
 pg_catalog         | postgres | postgres=UC/postgres+| system catalog schema
                    |          | =U/postgres          |
 pg_temp_1          | postgres |                      |
 pg_toast           | postgres |                      | reserved schema for TOAST tables
 pg_toast_temp_1    | postgres |                      |
 public             | postgres | postgres=UC/postgres+| standard public schema
                    |          | =UC/postgres         |
(6 rows)

任何想法如何解决这个问题?

【问题讨论】:

你确定你知道 django_migrations 在哪里以及谁拥有它吗?在 psql 中 \dt django_migrations 显示什么?还有 \dp django_migrations? 感谢@AdrianKlaver,我运行了这两个命令并将输出添加到刚才的问题中。有趣的是,我有另一个 EC2 实例,我的另一个 Django 项目运行得非常好,这两个命令显示的结果完全相同。 尝试:SHOW search_path; \dt *.django_migrations 刚才添加了这个命令的输出。 @AdrianKlaver 你能解决这个问题吗?我在尝试导入数据库以使项目在本地运行时遇到同样的问题。导入数据库转储后,也会得到django.db.utils.ProgrammingError: permission denied for table django_migrations 【参考方案1】:

请尝试使用以下命令,这对我有用

GRANT rds_superuser TO username;

【讨论】:

【参考方案2】:

我遇到了以下错误。

psycopg2.errors.InsufficientPrivilege:表 django_migrations 的权限被拒绝

将超级用户“postgres”的权限授予我正在使用的用户。

对我来说,这个命令有效:

GRANT postgres TO <user>;

【讨论】:

如果您有新问题,请点击 按钮提出问题。如果有助于提供上下文,请包含指向此问题的链接。 - From Review

以上是关于psycopg2.errors.InsufficientPrivilege:关系 django_migrations 的权限被拒绝的主要内容,如果未能解决你的问题,请参考以下文章