PostgreSQL 抛出错误:“错误:关系 table_name 的权限被拒绝”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3512247/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-20 00:15:29  来源:igfitidea点击:

PostgreSQL throws error: "ERROR: permission denied for relation table_name"

postgresqlimport

提问by dennis.winter

I'm trying to import a schema-dump into PostgreSQL with 'psql -U username -W dbname < migrations/schema.psql', the Schema is imported partly, but the console throws errors like "ERROR: permission denied for relation table_name" and "ERROR: relation table_name does not exist".

我正在尝试使用 'psql -U username -W dbname < migrations/schema.psql' 将架构转储导入 PostgreSQL,架构已部分导入,但控制台会抛出诸如“错误:关系表名称的权限被拒绝”之类的错误和“错误:关系表名称不存在”。

I've created the database like this: "createdb -O username dbname"

我已经创建了这样的数据库:“createdb -O username dbname”

There are only 7 tables to import, and it breaks with just importing 3 of them.

只有 7 个表要导入,并且只导入 3 个就中断了。

Anybody a hint, what to do?

任何人的提示,该怎么办?

采纳答案by Matthew Wood

If the backup was in the "custom" format (-Fc), you could use pg_restore instead so you can tell it to not apply ownership changes:

如果备份是“自定义”格式(-Fc),您可以使用 pg_restore 代替,这样您就可以告诉它不应用所有权更改:

pg_restore -U username -W --no-owner --dbname=dbname migrations/schema.psql

All the objects will created with "username" as the owner.

所有对象都将以“用户名”作为所有者创建。

Barring that, try to grep out the ALTER OWNER and SET SESSION AUTHORIZATION commands into new file (or through send grep output via pipe to psql). Those commands should always be on a single line in the plain-text output format.

除此之外,尝试将 ALTER OWNER 和 SET SESSION AUTHORIZATION 命令输出到新文件中(或通过管道将 grep 输出发送到 psql)。这些命令应始终以纯文本输出格式位于一行中。

回答by alvherre

Sometimes this kind of problem is caused by case-sensitivity issues. PostgreSQL folds to lowercase all unquoted identifiers; if the tables are created with quoted names containing uppercase letters, later commands that don't quote the names may fail to find the table.

有时这种问题是由区分大小写的问题引起的。PostgreSQL 将所有未加引号的标识符折叠为小写;如果表是用包含大写字母的带引号的名称创建的,则后面不带引号的命令可能无法找到该表。

The permission errors may be related to the same thing, or it may be something else entirely. Hard to tell without seeing the failing commands.

权限错误可能与同一件事有关,也可能完全不同。如果没有看到失败的命令,很难判断。

回答by Alexis Wilke

As I work on my websites, I get that error all the time because I'll be creating a table as me, table that needs to be accessed by the Apache/PHP user that connects later.

当我在我的网站上工作时,我总是遇到这个错误,因为我将像我一样创建一个表,该表需要由稍后连接的 Apache/PHP 用户访问。

There is a table named pg_class that defines your tables. This includes a column named relowner. Changing that with the correct number will give you the correct ownership.

有一个名为 pg_class 的表用于定义您的表。这包括名为 relowner 的列。用正确的数字改变它会给你正确的所有权。

I have details on this page:

我在此页面上有详细信息:

http://linux.m2osw.com/table_owner_in_postgresql

http://linux.m2osw.com/table_owner_in_postgresql

The ALTER OWNER ... may be a better solution that does the same thing, although in older versions of PostgreSQL it did notexist!

ALTER OWNER ... 可能是一个更好的解决方案,可以做同样的事情,尽管在旧版本的 PostgreSQL 中它存在!