简体   繁体   中英

PostgreSQL: Could not open file for reading. Permission denied. SQL state: 42501

COPY dept 
FROM '/media/rayhan/CSE/PostgreSQL/csv_file/dept.csv' 
DELIMITER ',' CSV HEADER;

Running this query produce following error. I've changed the file extension from .csv to .txt, but same error occurs. Help me to solve this problem. I'm using PostgreSQL version 9.5.12 . Thanks in advance.

ERROR: could not open file "/media/rayhan/CSE/PostgreSQL/csv_file/dept.txt" for reading: Permission denied

********** Error **********

ERROR: could not open file "/media/rayhan/CSE/PostgreSQL/csv_file/dept.csv" for reading: Permission denied SQL state: 42501

I was getting same issue when I was connecting from client to remote postgres server. I changed copy to \\copy and it worked.

In your case it would be

\\COPY dept FROM '/media/rayhan/CSE/PostgreSQL/csv_file/dept.csv' DELIMITER ',' CSV HEADER;

It seems that, The PostgreSQL process doesn't have the permission to access your "csv", find the username which started the PostgreSQL Service and then give the read permission to PG service user "using chmod" at Linux level.

If still facing issue then, change the permission as below:

chmod a+rX /media
chmod a+rX /media/rayhan
chmod a+rX /media/rayhan/CSE
chmod a+rX /media/rayhan/CSE/PostgreSQL
chmod a+rX /media/rayhan/CSE/PostgreSQL/csv_file
chmod a+rX /media/rayhan/CSE/PostgreSQL/csv_file/dept.csv

Now try again to use COPY.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM