简体   繁体   English

Postgres 错误:无法打开文件进行读取:权限被拒绝

[英]Postgres ERROR: could not open file for reading: Permission denied

Computer: Mac OS X, version 10.8 Database: Postgres计算机:Mac OS X,版本 10.8 数据库:Postgres

Trying to import csv file into postgres.试图将 csv 文件导入 postgres。

pg> copy items_ordered from '/users/darchcruise/desktop/items_ordered.csv' with CSV;
ERROR:  could not open file "/users/darchcruise/desktop/items_ordered.csv" for reading: Permission denied

Then I tried然后我尝试了

$> chown postgres /users/darchcruise/desktop/items_ordered.csv
chown: /users/darchcruise/desktop/items_ordered.csv: Operation not permitted

Lastly, I tried最后,我尝试了

$> ls -l
-rw-r--r--  1 darchcruise  staff      1016 Oct 18 21:04 items_ordered.csv

Any help is much appreciated!任何帮助深表感谢!

Assuming the psql command-line tool, you may use \copy instead of copy .假设psql命令行工具,您可以使用\copy而不是copy

\copy opens the file and feeds the contents to the server, whereas copy tells the server the open the file itself and read it, which may be problematic permission-wise, or even impossible if client and server run on different machines with no file sharing in-between. \copy打开文件并将内容提供给服务器,而copy告诉服务器打开文件本身并读取它,这可能在权限方面有问题,如果客户端和服务器在没有文件共享的不同机器上运行甚至是不可能的之间。

Under the hood, \copy is implemented as COPY FROM stdin and accepts the same options than the server-side COPY .在后台, \copy被实现为COPY FROM stdin并接受与服务器端COPY相同的选项。

Copy the CSV file to /tmp将 CSV 文件复制到 /tmp

For me this solved the issue.对我来说,这解决了这个问题。

chmod a+rX /users/darchcruise/ /users/darchcruise/desktop /users/darchcruise/desktop/items_ordered.csv

This will change access rights for your folder.这将更改您文件夹的访问权限。 Note that everyone will be able to read your file.请注意,每个人都可以阅读您的文件。 You can't use chown being a user without administrative rights.您不能使用 chown 作为没有管理权限的用户。 Also consider learning umask to ease creation of shared files.还可以考虑学习umask以简化共享文件的创建。

Copy your CSV file into the /tmp folder将 CSV 文件复制到 /tmp 文件夹

Files named in a COPY command are read or written directly by the server, not by the client application.在 COPY 命令中命名的文件由服务器直接读取或写入,而不是由客户端应用程序读取或写入。 Therefore, they must reside on or be accessible to the database server machine, not the client.因此,它们必须驻留在数据库服务器机器上或可供其访问,而不是客户端。 They must be accessible to and readable or writable by the PostgreSQL user (the user ID the server runs as), not the client.它们必须可由 PostgreSQL 用户(服务器运行的用户 ID)访问和读写,而不是客户端。 COPY naming a file is only allowed to database superusers, since it allows reading or writing any file that the server has privileges to access. COPY 命名文件只允许数据库超级用户使用,因为它允许读取或写入服务器有权访问的任何文件。

I had the issue when I was trying to export data from a remote server into the local disk.当我尝试将数据从远程服务器导出到本地磁盘时遇到了问题。 I hadn't realised that SQL copy actually is executed on the server and that it tries to write to a server folder.我没有意识到 SQL copy实际上是在服务器上执行的,并且它试图写入服务器文件夹。 Instead the correct thing to do was to use \copy which is the psql command and it writes to the local file system as I expected.相反,正确的做法是使用\copy这是 psql 命令,它按照我的预期写入本地文件系统。 http://www.postgresql.org/message-id/CAFjNrYsE4Za_KWzmfgN1_-MG7GTw_vpMRxPk=OEjAiLqLskxdA@mail.gmail.com http://www.postgresql.org/message-id/CAFjNrYsE4Za_KWzmfgN1_-MG7GTw_vpMRxPk=OEjAiLqLskxdA@mail.gmail.com

Perhaps that might be useful to someone else too.也许这对其他人也可能有用。

Another way to do this, if you have pgAdmin and are comfortable using the GUI is to go the table in the schema and right click on the table you wish to import the file to and select "Import" browse your computer for the file, select the type your file is, the columns you want the data to be imputed into, and then select import.另一种方法是,如果您有 pgAdmin 并且习惯于使用 GUI,则转到架构中的表并右键单击要将文件导入到的表并选择“导入”浏览您的计算机以查找文件,选择您的文件类型,您希望将数据估算到的列,然后选择导入。

That was done using pgAdmin III and the 9.4 version of PostgreSQL这是使用 pgAdmin III 和 PostgreSQL 的 9.4 版本完成的

I resolved the same issue with a recursive chown on the parent folder:我用父文件夹上的递归 chown 解决了同样的问题:

sudo chown -R postgres:postgres /home/my_user/export_folder

(my export being in /home/my_user/export_folder/export_1.csv ) (我的出口在/home/my_user/export_folder/export_1.csv

COPY your table (Name, Latitude, Longitude) FROM 'C:\Temp\your file.csv' DELIMITERS ',' CSV HEADER;

使用c:\Temp\"Your File"\

For me it worked to simply to add sudo (or run as root) for the chown command:对我来说,它可以简单地为 chown 命令添加 sudo (或以 root 身份运行):

sudo chown postgres /users/darchcruise/desktop/items_ordered.csv sudo chown postgres /users/darchcruise/desktop/items_ordered.csv

for macbook first i opened terminal then type对于macbook,我首先打开终端然后输入

open /tmp

or in finder directory you directly enter command+shift+g then type /tmp in go to the folder.或者在finder目录中直接输入command+shift+g然后输入/tmp进入文件夹。

it opens temp folder in finder.它在finder中打开临时文件夹。 then i paste copied csv file into this folder.then again i go to postgres terminal and typed below command and then it is copied my csv data into db table然后我将复制的 csv 文件粘贴到这个文件夹中。然后我再次转到 postgres 终端并输入以下命令,然后将我的 csv 数据复制到 db 表中

\copy recharge_operator FROM '/private/tmp/operator.csv' DELIMITER ',' CSV;

You must grant the pg_read_server_files permission to the user if you are not using postgres superuser .如果您不使用postgres superuser用户,则必须向用户授予pg_read_server_files权限。

Example:例子:

GRANT pg_read_server_files TO my_user WITH ADMIN OPTION;

万一您在 Windows 10 下遇到此问题,请在安全选项卡上添加用户组“youcomputer\Users”并授予它完全控制权,这解决了我的问题

I had the same error message but was using psycopg2 to communicate with PostgreSQL.我有同样的错误信息,但使用psycopg2与 PostgreSQL 通信。 I fixed the permission issues by using the functions copy_from and copy_expert that will open the file on the client side as the user running the python script and feed the data to the database over STDIN .我通过使用函数copy_fromcopy_expert修复了权限问题,这些函数将在用户运行 python 脚本时在客户端打开文件,并通过STDIN将数据提供给数据库。

Refer to this link for further information.有关详细信息,请参阅此链接

This answer is only for Linux Beginners.此答案仅适用于 Linux 初学者。

Assuming initially the DB user didn't have file/folder(directory) permission on the client side.假设最初数据库用户在客户端没有文件/文件夹(目录)权限。

Let's constrain ourselves to the following:让我们约束自己:

User: postgres用户: postgres

Purpose: You wanted to (write to / read from) a specific folder目的:您想要(写入/读取)特定文件夹

Tool: psql工具: psql

Connected to a specific database: YES连接到特定数据库: YES

FILE_PATH: /home/user/training/sql/csv_example.csv文件路径: /home/user/training/sql/csv_example.csv

Query: \copy (SELECT * FROM table_name TO FILE_PATH, DELIMITER ',' CSV HEADER;查询: \copy (SELECT * FROM table_name TO FILE_PATH, DELIMITER ',' CSV HEADER;

Actual Results: After running the query you got an error : Permission Denied实际结果:运行查询后出现错误: Permission Denied

Expected Results: COPY COUNT_OF_ROWS_COPIED预期结果: COPY COUNT_OF_ROWS_COPIED

Here are the steps I'd follow to try and resolve it.以下是我尝试解决它的步骤。

  1. Confirm the FILE_PATH permissions on your File system.确认文件系统上的 FILE_PATH 权限。

Inside a terminal to view the permissions for a file/folder you need to long list them by entering the command ls -l .在终端内查看文件/文件夹的权限,您需要通过输入命令ls -l来长列它们。

The output has a section that shows sth like this -> drwxrwxr-x Which is interpreted in the following way:输出有一个显示这样的部分 -> drwxrwxr-x以下列方式解释:

TYPE |类型 | OWNER RIGHTS |所有者权利 | GROUP RIGHTS |集团权利 | USER RIGHTS用户权利

rwx (r: Read, W: Write, X: Execute) rwx (r:读取,W:写入,X:执行)

TYPE (1 Char) = d: directory, -: file TYPE (1 Char) = d: 目录, -: 文件

OWNER RIGHTS (3 Chars after TYPE)所有者权利(TYPE 后 3 个字符)

GROUP RIGHTS (3 Chars after OWNER)组权(所有者后 3 个字符)

USER RIGHTS (3 Chars after GROUP)用户权利(组后 3 个字符)

  1. If permissions are not enough (Ensure that a user can at least enter all folders in the path you wanted path) - x .如果权限不够(确保用户至少可以输入您想要的路径中的所有文件夹) - x

This means for FILE_PATH, All the directories (home , user, training, sql) should have at least an x in the USER RIGHTS.这意味着对于 FILE_PATH,所有目录(home、user、training、sql)都应该在 USER RIGHTS 中至少有一个x

  1. Change permissions for all parent folders that you need to enter to have a x .更改您需要输入以具有x的所有父文件夹的权限。 You can use chmod rights_you_want parent_folder您可以使用chmod rights_you_want parent_folder

Assuming /training/ didn't have an execute permission.假设/training/没有执行权限。

I'd go the user folder and enter chmod a+x training我会去用户文件夹并输入chmod a+x training

  1. Change the destination folder/directory to have a w if you want to write to it.如果要写入目标文件夹/目录,请将其更改为w or at least a r if you want to read from it或者至少一个r如果你想从中读取

Assuming /sql didn't have a write permission.假设/sql没有写权限。

I would now chmod a+w sql我现在会chmod a+w sql

  1. Restart the postgresql server sudo systemctl restart postgresql重启 postgresql 服务器sudo systemctl restart postgresql
  2. Try again.再试一次。

This would most probably help you now get a successful expected result.这很可能会帮助您现在获得成功的预期结果。

On Linux you can fix this by giving the postgres user read/write/execute permissions on the target directory.在 Linux 上,您可以通过授予postgres用户对目标目录的读/写/执行权限来解决此问题。 Eg:例如:

setfacl -m u:postgres:rwx /home/hi

我刚刚将源 csv 文件复制到外部 USB 驱动器,它工作正常。

可能是您正在通过连接远程主机使用 pgadmin,然后您正在尝试从您的系统进行更新,但它在远程系统的文件系统中搜索该文件......这是我面临的错误可能也是您检查它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pgadmin 错误:无法打开文件读取:权限被拒绝 - Pgadmin ERROR: could not open file reading: Permission denied Postgres FATAL:无法打开日志文件权限被拒绝 - Postgres FATAL: could not open log file permission denied 错误:无法打开文件进行读取:权限被拒绝。 SQL 状态:42501 - ERROR: could not open file for reading: Permission denied. SQL state: 42501 错误:无法打开文件“ /root/Data.csv”进行读取:权限被拒绝 - error: could not open file “/root/Data.csv” for reading: Permission denied Postgres 错误:无法找到全局文件结尾/1262:权限被拒绝 - Postgres ERROR: could not seek to end of file global/1262: Permission denied PostgreSQL:无法打开文件进行读取。 没有权限。 SQL 状态:42501 - PostgreSQL: Could not open file for reading. Permission denied. SQL state: 42501 postgres 无法统计文件<basexxxx/xxxx>没有权限 - postgres could not stat file <basexxxx/xxxx> Permission denied 安装 Postgres 自定义类型 - 无法访问文件“...”:权限被拒绝 - Installing Postgres custom type - could not access file "...": Permission denied 无法打开基本文件“base/13322/1259”:权限被拒绝 - could not open base file "base/13322/1259":Permission denied Postgres 错误:表的权限被拒绝 - Postgres ERROR: permission denied for table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM