1

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?
Community
  • 1
  • 1
hrskrs
  • 4,447
  • 5
  • 38
  • 52

7 Answers7

1
  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.
yieldsfalsehood
  • 3,005
  • 1
  • 19
  • 14
  • @yieldsfalsehodd I was searching for varchar when creating new column, but there were ne varchar. Anyway i made them 'character' as u suggested. However still getting the same error about file permission. The file is closed at all (and yeah i converted to .csv by doing Save As). – hrskrs Jan 09 '14 at 17:22
1

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.

Alfabravo
  • 7,493
  • 6
  • 46
  • 82
  • I checked all man...User have all the priviledges. I gave also all public privileges to db, table and columns seperately again same error ! – hrskrs Jan 09 '14 at 17:45
0
  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.
wonhee
  • 1,581
  • 1
  • 14
  • 24
  • tried smth like: copy allfields from 'C:\filepath\filename.csv' DELIMITER ';' CSV HEADER also user have all the privileges. I created a new role and made it as superuser.. same error ! – hrskrs Jan 09 '14 at 17:47
0

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.

Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
0

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

jjanes
  • 37,812
  • 5
  • 27
  • 34
  • What are you using as the front end client to issue the COPY? Other tools have other ways to accomplish the same thing. – jjanes Jan 09 '14 at 21:13
0

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.

hrskrs
  • 4,447
  • 5
  • 38
  • 52
0

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.
Caleb Rotich
  • 603
  • 5
  • 11