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.

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'

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.

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.

Rajkishore Sahoo is a Microsoft Azure consultant with more than 14 years of hands-on experience building cloud-native data solutions across Azure and AWS, focused on Azure SQL Database, Azure SQL Managed Instance, and the pipelines that connect them to production apps. Day to day, he provisions Azure SQL instances, configures firewalls and connection strings, tunes queries for cloud workloads, and wires databases into Azure Functions and Logic Apps. Read more