1
votes

I have seen questions on stackoverflow similar/same as the one I am asking now, however I couldn't manage to solve it in my situation.

Here is the thing: I have an excel spreadsheet(.xlsx) whom i converted in comma seperated value(.CSV) as it is said in some answers:

My excel file looks something like this:

--------------------------------------------------
name  |  surname | voteNo  | VoteA | VoteB | VoteC
--------------------------------------------------
john  |  smith   | 1001    | 30    | 154   | 25
--------------------------------------------------
anothe|  person  | 1002    | 430   | 34    | 234
--------------------------------------------------
other |  one     | 1003    | 35    | 154   | 24
--------------------------------------------------
john  |  smith   | 1004    | 123   | 234   | 53
--------------------------------------------------
john  |  smith   | 1005    | 23    | 233   | 234
--------------------------------------------------

In PostgreSQL I created a table with name allfields and created 6 columns 1st and 2nd one as a character[] and last 4 ones as integers with the same name as shown in the excel table (name, surname, voteno, votea, voteb, votec)

Now I'm doing this:

copy allfields from 'C:\Filepath\filename.csv';

But I'm getting this error:

could not open file "C:\Filepath\filename.csv" for reading: Permission denied
SQL state: 42501

My questions are:

  1. Should I create those columns in allfields table in PostgreSQL?
  2. Do I have to modify anything else in Excel file?
  3. And why I get this 'permission denied' error?
7

7 Answers

1
votes
  1. Based on your file, neither of the first two columns needs to be an array type (character[]) - unlike C-strings, the "character" type in postgres is a string already. You might want to make things easier and use varchar as the type of those two columns instead.
  2. I don't think you do.
  3. Check that you don't still have that file open and locked in excel - if you did a "save as" to convert from xlsx to csv from within excel then you'll likely need to close out the file in excel.
1
votes

SQL state: 42501 in PostgreSQL means you don't have permission to perform such operation in the intended schema. This error code list shows that.

Check that you're pointing to the correct schema and your user has enough privileges.

Documentation also states that you need select privileges on origin table and insert privileges on the destination table.

You must have select privilege on the table whose values are read by COPY TO, and insert privilege on the table into which values are inserted by COPY FROM. It is sufficient to have column privileges on the column(s) listed in the command.

0
votes
  1. Yes I think you can. For COPY command, there is optional HEADER clause. Check http://www.postgresql.org/docs/9.2/static/sql-copy.html
  2. I don't think so. With my #1 and #3, it should works.
  3. You need superuser permission for that.
0
votes

1) Should I create those columns in allfields table in PostgreSQL?

Use text for the character fields. Not an array in any case, as @yieldsfalsehood pointed out correctly.

2) Do I have to modify anything else in Excel file?

No.

3) And why I get this 'permission denied' error?

The file needs be accessible to your system user postgres (or what ever user you are running the postgres server with). Per documentation:

COPY with a file name instructs the PostgreSQL server to directly read from or write to a file. The file must be accessible to the server and the name must be specified from the viewpoint of the server.

The privileges of the database user are not the cause of the problem. However (quoting the same page):

COPY naming a file or command is only allowed to database superusers, since it allows reading or writing any file that the server has privileges to access.

0
votes

Regarding the permission problem, if you are using psql to issue the COPY command, try using \copy instead.

0
votes

Ok the Problem was that i need to change the path of the Excel file. I inserted it in the public account where all users can access it.

If you face the same problem move your excel file to ex C:\\User\Public folder (this folder is a public folder without any restrictions), otherwise you have to deal with Windows permission issues.

0
votes

For those who do not wish to move the files they wish to read to a different location(public) for some reason. Here is a clear solution.

  1. Right click the folder holding the file and select properties.
  2. Select the Security tab under properties.
  3. Select Edit
  4. Select Add
  5. Under the field Enter the object Names to select, Type in Everyone
  6. Click OK to all the dialog boxes or Apply if it is activated
  7. Try reading the file again.