Psql import SQL file permission denied

Recently, when I attempted to import the SQL file, I encountered the ‘permission denied’ error. First, let’s examine the error that occurs during SQL file import, and then we will discuss the solution to resolve this issue.

Psql import SQL file permission denied

For Windows users, enter the psql command prompt by using the command below. If it asks for a password, enter the password for the psql user.

psql -U postgres

Now, suppose we have an SQL file on our computer, such as D:\Backup\name.sql, and we want to import it. For this, use the command below.

\i D:\Backup\name.sql

After running the above command, we encounter an error, ‘Permission denied,’ as shown in the screenshot below.

Psql import SQL file permission denied

Solution

For Windows users

To solve the error for Windows users, use the double slashes ( \\ ) in the place of single slashes ( \ ) and wrap the path of the SQL file within a single quotation mark ( ‘ ‘ ). Use the below command.

\i 'D:\\Backup\\name.sql'
Psql import SQL file permission denied

In the above output, we have successfully imported the SQL file, created the table with some data, and resolved the ‘Permission denied’ issue. Please refer to the screenshot above for your reference.

For Linux users

For Linux users, log in to PostgreSQL as a Postgres user to access the databases and then enter the psql prompt using the below command:

sudo su -l postgres 

-- after the above command enter the psql command

psql

-- then enter the below command to import the SQL file

\i '//home//saurabh//database.sql

After executing the above query, I got the expected output as per the screenshot below.

Psql import SQL file permission denied

Use the double slashes ( // ) in the place of single slashes ( / ) and wrap the path of the SQL file within a single quotation mark ( ‘ ‘ ). Use the below command.

We have also now resolved the error for Linux systems.

Conclusion

In this article, we discussed the solution to fix the error “Psql import SQL file permission denied”.

You may also like following the related articles.

Top 200 SQL Server Interview Questions and Answers

Free PDF On Top 200 SQL Server Interview Questions And Answers

Download A 40 pages PDF And Learn Now.