Computer: Mac OS X, version 10.8 Database: Postgres
Trying to import csv file into postgres.
pg> copy items_ordered from \'/users/darchcruise/desk
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;
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
.
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
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)
x
.This means for FILE_PATH, All the directories (home , user, training, sql) should have at least an x
in the USER RIGHTS.
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
w
if you want to write to it. or at least a r
if you want to read from itAssuming /sql
didn't have a write permission.
I would now chmod a+w sql
sudo systemctl restart postgresql
This would most probably help you now get a successful expected result.
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.
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.
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.