使用 \i 命令从文件读取时拒绝 postgreSQL 权限

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

postgreSQL permission denied when reading from file with \i command

postgresql

提问by Euler's formula

this is my problem:

这是我的问题:

myname@ubuntu:~$ sudo su postgres -c "psql template1"
Password: 
psql (9.1.6)
Type "help" for help.

template1=# \i /create.sql
/create.sql: Permission denied
myname@ubuntu:~$ sudo su postgres -c "psql template1"
Password: 
psql (9.1.6)
Type "help" for help.

template1=# \i /create.sql
/create.sql: Permission denied

I have this problem even when the file is on the Desktop. when I copy the text of create.sql and paste it there it works.

即使文件在桌面上,我也有这个问题。当我复制 create.sql 的文本并将其粘贴到那里时,它可以工作。

using UBUNTU 12.10, postgresql 9.1

使用 UBUNTU 12.10,postgresql 9.1

Thank you for the help.

感谢您的帮助。

回答by Craig Ringer

The problem is that you've launched psqlas the user postgresand you haven't granted the postgresuser permission to read the SQL file. You'll need to give it permission. The simplest way is to grant world read rights:

问题是您已经psql以用户身份启动,postgres并且您还没有授予postgres用户读取 SQL 文件的权限。你需要给它许可。最简单的方法是授予全局读取权限:

chmod a+r create.sql

You can change the default permissions assigned to files by altering your umask; search for more information. Some programs rather annoyingly ignore the umaskand set restrictive file permissions anyway, so you always need to know how to grant permissions. See man chmodand man chown.

您可以通过更改您的umask;来更改分配给文件的默认权限。搜索更多信息。一些程序相当烦人地忽略umask并设置限制性文件权限,因此您总是需要知道如何授予权限。见man chmodman chown

BTW, you're using a very convoluted method for launching psqlas the postgresuser. This'll do just fine:

顺便说一句,您正在使用一种非常复杂的方法psql作为postgres用户启动。这会做得很好:

sudo -u postgres psql template1


Note that /create.sqlspecifies a file named create.sqlin root of the file system (/). I doubt this is what you intended.

请注意,/create.sql指定以create.sql文件系统 ( /) 的根目录命名的文件。我怀疑这就是你的意图。

You probably want to specify it as a relative path (without the leading /) if it's in your home directory, like:

/如果它在您的主目录中,您可能希望将其指定为相对路径(没有前导),例如:

template1=# \i create.sql

or if it's on the desktop and you've started psqlin your home directory:

或者如果它在桌面上并且您已经psql在您的主目录中启动:

template1=# \i Desktop/create.sql