8

this is my problem:

myname@ubuntu:~$ sudo su postgres -c "psql template1"
Password: 
psql (9.1.6)
Type "help" for help.

template1=# \i /create.sql
/create.sql: Permission denied

I have this problem even when the file is on the Desktop. when I copy the text of create.sql and paste it there it works.

using UBUNTU 12.10, postgresql 9.1

Thank you for the help.

Euler's formula
  • 83
  • 1
  • 1
  • 4

6 Answers6

17

If you work in windows , you just need to pass the entire path wrapped by a single quotation.

test-# \i 'D:\\person.sql' --- > note here double backslash not single
Sihat Afnan
  • 742
  • 7
  • 14
13

The problem is that you've launched psql as the user postgres and you haven't granted the postgres user permission to read the SQL file. You'll need to give it permission. The simplest way is to grant world read rights:

chmod a+r create.sql

You can change the default permissions assigned to files by altering your umask; search for more information. Some programs rather annoyingly ignore the umask and set restrictive file permissions anyway, so you always need to know how to grant permissions. See man chmod and man chown.

BTW, you're using a very convoluted method for launching psql as the postgres user. This'll do just fine:

sudo -u postgres psql template1

Note that /create.sql specifies a file named create.sql in root of the file system (/). I doubt this is what you intended.

You probably want to specify it as a relative path (without the leading /) if it's in your home directory, like:

template1=# \i create.sql

or if it's on the desktop and you've started psql in your home directory:

template1=# \i Desktop/create.sql
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
1

EDIT: A better solution is to move the SQL file to the tmp folder if you are on linux!

I ran into this same issue on Linux but the accepted solution was not sufficient in my case. I eventually realized that you need to make sure that EVERY folder in the path has the correct permissions.

For example if the home folder does not have the correct permissions it does not matter what permissions you give a file inside of the SQL folder.

/home/username/Documents/SQL 
Joe Casey
  • 43
  • 5
0

if you are facing same issue after putting quote then use forward slash for paths

0

The problem of permission can be solved by changing the permission of every subdirectory.

chmod og+rX /home /home/user

This should resolve the problem.

P.S. There could be a risk of security as the permission of these files is set to others for read and execution

Asad
  • 1
  • 2
0

It works because you are basically executing the SQL statements as the current user and not reading the file when you copy and paste the entire content of create.sql straight into the terminal. On the other hand, file access permissions are essential in order to use the \i command in psql.

Make sure that the user using the psql command is allowed to access the file /create.sql and that it has the appropriate permissions. The ls -l command can be used to ensure the permissions:

ls -l /create.sql

Ensure that the user executing the psql command is able to read the file. Using the chmod command, you can modify these permissions if you find them very restrictive:

sudo chmod +r /create.sql

It could also be problematic if the file is kept in a directory with restrictive permissions. Make sure that the necessary permissions are set for the parent directory of /create.sql:

ls -ld /path/to/parent_directory