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.
- 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)
- 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.
- 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
- 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
- Restart the postgresql server
sudo systemctl restart postgresql
- Try again.
This would most probably help you now get a successful expected result.