[英]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_from
和copy_expert
修复了权限问题,这些函数将在用户运行 python 脚本时在客户端打开文件,并通过STDIN
将数据提供给数据库。
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
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 个字符)
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
。
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
w
if you want to write to it.w
。 or at least a r
if you want to read from itr
如果你想从中读取Assuming /sql
didn't have a write permission.假设
/sql
没有写权限。
I would now chmod a+w sql
我现在会
chmod a+w sql
sudo systemctl restart postgresql
sudo systemctl restart postgresql
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.