Postgres ERROR: could not open file for reading: Permission denied

前端 未结 15 2265
慢半拍i
慢半拍i 2020-12-04 13:15

Computer: Mac OS X, version 10.8 Database: Postgres

Trying to import csv file into postgres.

pg> copy items_ordered from \'/users/darchcruise/desk         


        
相关标签:
15条回答
  • 2020-12-04 13:30

    You must grant the pg_read_server_files permission to the user if you are not using postgres superuser.

    Example:

    GRANT pg_read_server_files TO my_user WITH ADMIN OPTION;
    
    0 讨论(0)
  • Assuming the psql command-line tool, you may use \copy instead of 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.

    Under the hood, \copy is implemented as COPY FROM stdin and accepts the same options than the server-side COPY.

    0 讨论(0)
  • 2020-12-04 13:34

    This answer is only for Linux Beginners.

    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

    Purpose: You wanted to (write to / read from) a specific folder

    Tool: psql

    Connected to a specific database: YES

    FILE_PATH: /home/user/training/sql/csv_example.csv

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

    Actual Results: After running the query you got an error : Permission Denied

    Expected Results: 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.

    Inside a terminal to view the permissions for a file/folder you need to long list them by entering the command ls -l.

    The output has a section that shows sth like this -> drwxrwxr-x Which is interpreted in the following way:

    TYPE | OWNER RIGHTS | GROUP RIGHTS | USER RIGHTS

    rwx (r: Read, W: Write, X: Execute)

    TYPE (1 Char) = d: directory, -: file

    OWNER RIGHTS (3 Chars after TYPE)

    GROUP RIGHTS (3 Chars after OWNER)

    USER RIGHTS (3 Chars after GROUP)

    1. If permissions are not enough (Ensure that a user can at least enter all folders in the path you wanted path) - x.

    This means for FILE_PATH, All the directories (home , user, training, sql) should have at least an x in the USER RIGHTS.

    1. Change permissions for all parent folders that you need to enter to have a x. You can use chmod rights_you_want parent_folder

    Assuming /training/ didn't have an execute permission.

    I'd go the user folder and enter chmod a+x training

    1. Change the destination folder/directory to have a w if you want to write to it. or at least a r if you want to read from it

    Assuming /sql didn't have a write permission.

    I would now chmod a+w sql

    1. Restart the postgresql server sudo systemctl restart postgresql
    2. Try again.

    This would most probably help you now get a successful expected result.

    0 讨论(0)
  • 2020-12-04 13:38
    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 umask to ease creation of shared files.

    0 讨论(0)
  • 2020-12-04 13:38

    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

    Perhaps that might be useful to someone else too.

    0 讨论(0)
  • 2020-12-04 13:40

    Copy your CSV file into the /tmp folder

    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.

    0 讨论(0)
提交回复
热议问题