In PostgreSQL, the “COPY” command allows us to import or export data to and from a database. CSV files are commonly used when exchanging data between applications or systems. It is a fast and efficient way of loading gigantic amounts of data into the Postgres table. The “COPY” statement reads data from a CSV file or standard input and inserts it into a Postgres table. However, sometimes user’s encounter a “Permission Denied” error while importing a CSV file into a Postgres table.
This blog post will show you the possible causes and solutions for the “permission denied” error. So, let’s start!
How to Fix the “Permission Denied” Error While Importing a CSV File?
This section will show you a step-by-step procedure to fix the “permission denied” error.
Step 1: Creating a CSV File
Firstly, create a CSV file and specify some data in that file:
The above snippet shows a CSV file named "articles" that is saved at the following location: “C:\Users\DELL\Desktop”.
Step 2: Creating a Sample Table
Let’s create a sample table via the below-provided command:
CREATE TABLE articles_tab( a_id INT PRIMARY KEY, a_title TEXT, p_date DATE );
Execute the “SELECT *” command to verify the table’s creation:
The sample table has been created.
Step 3: How Does the “Permission Denied” Error Occur in Postgres?
Let’s learn how to import the CSV file into the Postgres table:
COPY articles_tab FROM 'C:\Users\DELL\Desktop\articles_data.txt' DELIMITER ',' CSV HEADER;
While executing the above command, you may encounter the following error:
The output shows that an error occurred while importing a CSV file.
Step 4: How to Fix the “Permission Denied” Error?
First, right-click on the targeted file and select “Properties”:
Once the “Properties” window is open, select the “Security” tab:
Now, hit the “Edit” button, and a new pop-up window will appear:
In the "Permissions" window, hit the “Add..” button:
Write “Everyone” in the "Object Names” text field and hit the OK button. Now select “everyone” from the “Permission” window, and tick the read permissions:
Finally, hit the “OK” button to save the settings.
Step 5: Import CSV File
Now, let’s try one more time to import the CSV file via the following command:
COPY articles_tab FROM 'C:\Users\DELL\Desktop\articles_data.txt' DELIMITER ',' CSV HEADER;
The output shows that the “Permission Denied” error has been resolved. You can verify the inserted data via the “SELECT *” command:
The output verifies that the COPY command executed successfully, and hence the data from the CSV file has been imported into the Postgres table successfully.
Note: the other possible reason can be executing the “COPY” command as an ordinary user. To fix this error, you must execute the COPY command as a superuser.
Conclusion
PostgreSQL provides a "COPY" command to import and export data. The “COPY” statement reads data from a CSV file or standard input and inserts it into a Postgres table. However, sometimes user’s encounter a “Permission Denied” error while importing a CSV file into a Postgres table. To fix this error, you need to change the file reading permissions, as demonstrated in this guide. This blog explained how to fix the “Permission Denied” error while importing a CSV file in PostgreSQL.