postgresql Postgres 错误:无法打开文件进行读取:权限被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19463074/
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
Postgres ERROR: could not open file for reading: Permission denied
提问by user2449984
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!
任何帮助深表感谢!
采纳答案by Basilevs
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. Also consider learning umaskto ease creation of shared files.
这将更改您的文件夹的访问权限。请注意,每个人都可以阅读您的文件。您不能使用 chown 作为没有管理权限的用户。还可以考虑学习umask以简化共享文件的创建。
回答by Daniel Vérité
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
。
回答by user637338
Copy the CSV file to /tmp
将 CSV 文件复制到 /tmp
For me this solved the issue.
对我来说,这解决了这个问题。
回答by Antonio
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. 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. 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 命令中命名的文件由服务器直接读取或写入,而不是由客户端应用程序读取或写入。因此,它们必须驻留在数据库服务器计算机上或可由其访问,而不是客户端。它们必须可由 PostgreSQL 用户(服务器运行的用户 ID)而不是客户端访问和读取或写入。COPY 命名文件只允许数据库超级用户使用,因为它允许读取或写入服务器有权访问的任何文件。
回答by nikola
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. 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. http://www.postgresql.org/message-id/CAFjNrYsE4Za_KWzmfgN1_-MG7GTw_vpMRxPk=OEjAiLqLskxdA@mail.gmail.com
当我尝试将数据从远程服务器导出到本地磁盘时遇到了问题。我没有意识到 SQLcopy
实际上是在服务器上执行的,并且它试图写入服务器文件夹。相反,正确的做法是使用\copy
which 是 psql 命令,它按照我的预期写入本地文件系统。http://www.postgresql.org/message-id/CAFjNrYsE4Za_KWzmfgN1_-MG7GTw_vpMRxPk=OEjAiLqLskxdA@mail.gmail.com
Perhaps that might be useful to someone else too.
也许这对其他人也可能有用。
回答by Jeff Spicoli
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 和 9.4 版本的 PostgreSQL 完成的
回答by Benjamin Crouzier
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
)
回答by Benderradji Kayer Dean
just in case you're facing this problem under windows 10 , add the group of users "youcomputer\Users" on the security Tab and grant it full control , that solved my issue
以防万一您在 Windows 10 下遇到此问题,请在安全选项卡上添加用户组“youcomputer\Users”并授予其完全控制权,这解决了我的问题
回答by Dinesh Gurjar
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. 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
它在finder中打开临时文件夹。然后我将复制的 csv 文件粘贴到此文件夹中。然后我再次转到 postgres 终端并输入以下命令,然后将我的 csv 数据复制到 db 表中
\copy recharge_operator FROM '/private/tmp/operator.csv' DELIMITER ',' CSV;
回答by annakeuchenius
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
须藤 chown postgres /users/darchcruise/desktop/items_ordered.csv