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

14

我的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;

我成功地通过以下命令将另一个 SQL 转储文件导入到新的数据库中:psql -U postgres fishercoder < fishercoder_dump.sql

然后我尝试在这个 EC2 实例上运行 ./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

我在 Stack Overflow 上找到了三篇相关的帖子:第一篇第二篇第三篇
我尝试了他们建议的命令:
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呢? - Adrian Klaver
尝试执行:SHOW search_path; \dt *.django_migrations - Adrian Klaver
刚刚将此命令的输出添加到问题中。@AdrianKlaver - Fisher Coder
1
你能解决这个问题吗?我也遇到了同样的问题,尝试导入数据库以在本地运行项目。同时,在导入数据库转储后,我还遇到了 django.db.utils.ProgrammingError: permission denied for table django_migrations 的错误。 - MadPhysicist
我遇到了同样的问题。但是在将数据库用户更改为超级用户之后,它对我起作用了。不知道这会不会对你们起作用。 - sharif_42
显示剩余5条评论
3个回答

22

我出现了以下错误:

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

我将超级用户 'postgres' 的特权授予正在使用的用户。

对我来说,这个命令可以解决问题:

GRANT postgres TO <user>;


如果您有新的问题,请通过单击提问按钮来提出。如果它有助于提供上下文,请包含此问题的链接。- 来自审核 - Beso
3
这是唯一对我起作用的东西。我想知道为什么。 - MadPhysicist

8

数据库恢复后,所有表都将由postgres用户拥有。

您需要执行以下授权操作:

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO <user>;

我遇到了同样的问题,并且解决了。

你还需要为Django用户授予其他权限:

GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO <user>;

0
请尝试使用以下命令,这对我有用:
GRANT rds_superuser TO username;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接