postgresql 对“django.db.utils.ProgrammingError: 关系 django_migrations 的权限被拒绝”进行故障排除的步骤
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38944551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Steps to Troubleshoot "django.db.utils.ProgrammingError: permission denied for relation django_migrations"
提问by user3062149
What are some basic steps for troubleshooting and narrowing down the cause for the "django.db.utils.ProgrammingError: permission denied for relation django_migrations" error from Django?
有哪些基本步骤可以排除 Django 的“django.db.utils.ProgrammingError: permission denied for relation django_migrations”错误并缩小原因范围?
I'm getting this message after what was initially a stable production server but has since had some changes to several aspects of Django, Postgres, Apache, and a pull from Github. In addition, it has been some time since those changes were made and I don't recall or can't track every change that may be causing the problem.
我在最初是一个稳定的生产服务器之后收到此消息,但此后对 Django、Postgres、Apache 的几个方面进行了一些更改,并从 Github 中提取了一些内容。此外,进行这些更改已经有一段时间了,我不记得或无法跟踪可能导致问题的每个更改。
I get the message when I run python manage.py runserver
or any other python manage.py ...
command except python manage.py check
, which states the system is good.
我在运行python manage.py runserver
或python manage.py ...
除 之外的任何其他命令时收到消息python manage.py check
,表明系统良好。
回答by user3062149
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:
我能够根据这个问题的说明解决我的问题。基本上,需要将 postgres 权限重新授予 db 用户。就我而言,那是我在虚拟环境设置文件中设置的用户。从命令行(或在 postgres 中)运行以下命令,其中mydatabase
和dbuser
应该是您自己的数据库和用户名:
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;"
回答by Brad Solomon
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
正如@user3062149 所提到的,这可能是由于尝试迁移 Django 的 psycopg2 用户不是表所有者的数据库表引起的。例如,如果您在项目的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.
您需要检查 Django 迁移中涉及的表是否归my_username
. 为此psql
,您可以使用SELECT * FROM pg_tables ORDER BY tableowner;
. 这使用视图pg_tables
,它“提供对数据库中每个表的有用信息的访问”。 pg_tables
是 Postgres系统目录的一部分,是关系数据库管理系统存储模式元数据的地方。
Say that the table in question is owned by other_username
(not my_username
).
假设有问题的表归other_username
(not my_username
) 所有。
To update the owner, you then need to call psql
with --username=other_username
, then change the owner:
要更新所有者,那么你需要调用psql
带--username=other_username
,然后更改所有者:
ALTER TABLE public.<table_name> OWNER TO my_username;