Postgresql 有文件权限但说没有

Postgresql has permission to file but says it does not

我在 Fedora 22 上从 PostgreSQL 9.4.4 得到非常奇怪的行为:

我正在尝试 运行 来自 psql 的查询:

su - postgres
psql dbName
COPY sometable FROM '/the/file/location.csv' WITH DELIMITER AS ',' CSV;

它给我这个错误:

could not open file "/the/file/location.csv" for reading: Permission denied

但是,如果我这样做:

su - postgres
nano /the/file/location.csv

它按预期打开文件,因为整个路径对所有用户都有 read/execute 权限。

有人知道为什么会这样吗?

编辑:我现在甚至尝试将文件移动到 /tmp/file.csv,将文件的所有权更改为 postgres并关闭 SELinux。还是一样的错误。

编辑 2:我忘了说这是一个主从设置,我从从服务器通过 SSH 连接到主服务器。否则设置工作正常。

编辑 3:我收回我最初所说的话。关闭 SELinux 已解决问题。我现在需要找出一种使用它的方法,以便正确解决此问题。

这是一个 SELinux 问题。该文件需要 postgresql_db_t 标签。

这可以通过 运行 对单个文件执行此命令来完成:

chcon --reference /var/lib/pgsql/data/ /the/file/location.csv

或此命令用于整个目录

chcon -R --reference /var/lib/pgsql/data/ /the/directory/

这些命令告诉 SELinux 将 PosgresSQL label/right 从 PostgreSQL 的默认目录复制到 PosgreSQL 被拒绝访问的 file/directory。

这可以用 ls -lZ 检查,之后应该看起来像这样:

-rwxrwxrwx. 1 postgres postgres system_u:object_r:postgresql_db_t:s0 0 Jul 26 00:00 fileName.csv