Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permission denied when trying to import a CSV file from PGAdmin

I'm using PGAdmin 1.14.3.

When I try to execute an import command:

COPY grad(country_code, postal_code, place_name, admin_name1, admin_code1, admin_name2, admin_code2, admin_name3, admin_code3, latitude, longitude, accuracy)
FROM 'C:\\Users\\denis\\Desktop\\BP2Project\\USA\\US.txt';

I get a

ERROR: could not open file "C:\Users\denis\Desktop\BP2Project\USA\US.txt" for reading: Permission denied SQL state: 42501

I did look up other similar questions and none of them solved my issue.

I logged in as user "postgres" who is the superuser. I don't see why I'm missing permissions. I'm on Windows 7.

like image 300
Tool Avatar asked Dec 29 '12 15:12

Tool


People also ask

How do I fix Postgres permission denied?

Grant privileges to a new user We resolve this permission denied error using the command. GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO new_user; The new_user was then able to read data from the table. Similarly, we can also resolve the permission denied error by setting DEFAULT privileges to the user.

How do I import a CSV file into PgAdmin?

To import CSV using this PgAdmin Import CSV method, you have to do the following: Click on the Tools tab at the top of your PgAdmin Home Page. Select the Query Tool in the drop-down menu that appears. Enter the title and columns in your CSV file as an SQL Query.


6 Answers

The permissions article mentioned in the answer by Houari and Flimzy is a good reference material, but a direct answer (the quick fix I used) is:

  • Right click the folder containing the data file(s) that permission was denied to and then click Properties.
  • In the Folder's Properties window, select the Security tab.
  • Click the Edit button.
  • In the "Permissions for the folder" window that opened, click the Add... button.
  • Type Everyone into the "Enter the object names to select" text area box.
  • Click OK and the window will close.
  • Verify that the default Read & Execute permissions were set to Allow via the check checkbox in the previous window.
  • Click OK and the window will close.
  • Click the Apply button in the Folder Properties window.

Now you can run the SQL COPY statement that needs to access those files.

  • Once done, return to the Folder's Properties window.
  • Click the Edit button.
  • Select the Everyone entry in the "Group or user names:" field.
  • Click the Remove button.
  • Click OK on the remaining open windows.

The permissions have now been returned to what they were.

like image 178
Kevin Avatar answered Oct 04 '22 17:10

Kevin


The user Postgres must have read access on the file from which you are about to copy.

Look at this article to see how to modify files' security access on Windows.

like image 27
Houari Avatar answered Oct 04 '22 18:10

Houari


Ok, this is how got COPY command working,to export a table to CSV, step by step. Pls note that I am using pgAdmin 111.

  1. Create the target folder you want to export a table to. E.g C:\myExports
  2. Set a read/write permission on this folder following the steps below :

Right click the folder containing the data file(s) that permission was denied >to and then click Properties.

In the Folder's Properties window, select the Security tab. Click the Edit button.

In the "Permissions for the folder" window that opened, click the Add... button. Type Everyone into the "Enter the object names to select" text area box.

Click OK and the window will close. Verify that the default Read & Execute permissions were set to Allow via the >check checkbox in the previous window. Click OK and the window will close.

Click the Apply button in the Folder Properties window.

  1. This is the tricky part, inside myExports folder create a blank CSV file with your desired name.E.g employee.csv

  2. Then run the Copy command like this :

    copy employee to 'C:\myExports\employee.csv' delimiter ',' csv;

employee is the table name in this example..

Hope this helps.

like image 39
folorunso joseph Avatar answered Oct 04 '22 17:10

folorunso joseph


If you don't want to give permissions to Everyone, you can add permissions to the account that started the service. In the Control Panel - Administrative Tools - Services, copy the account name in the 'Log On' tab. (On my system the account is called 'Network Service'.) Then share the folder with the CSV-file with this user as shown in the answer above.

like image 40
Jonas B Avatar answered Oct 04 '22 19:10

Jonas B


To solve this problem you must give permission to the CSV file because that CSV file present in a COPY command are read directly by the server, but not client application. So to make this file accessible to a server we must give full read-write permission so that Postgresql user can read and write on that file.

Reference: article showing step by step procedure.

like image 32
Diwas Poudel Avatar answered Oct 04 '22 18:10

Diwas Poudel


I just ran into this error and even after adding postgres to permissions on the file folder and the file itself, it still didn't work. So, I put the file in a public folder. On Windows this was the path: "C:\Users\Public\Documents\census.csv." It worked!

like image 29
Debra Ray Avatar answered Oct 04 '22 18:10

Debra Ray