0

I am trying to restore a database with the following command:

$ sudo -u postgres pg_restore -C -d dvdrental dvdrental.tar 
[sudo] password for t: 

However, I am receiving the following error message:

could not change directory to "/home/t/mydata/.../relation model/SQL/implementations/Implementations, i.e. relational database management systems/postgreSQL/general/web/postgresqltutorial/databases": Permission denied

pg_restore: [archiver] could not open input file "dvdrental.tar": No such file or directory

I was wondering why I can't change directory to the current directory with permission denied?

File permission bits are:

 -rw-rw-r-- 1 t t 2838016 May 26  2013 dvdrental.tar

Is it because one of its ancestry directory is not both readable and executable by any one? The file has many ancestry directories, and how can I verify that?

2

The current directory, and all its parent directories, have to be accessible for the postgres user, i.e. have the executable/searchable bit set for whichever owner/group/other permission applies on each directory when determining postgres’s permissions, or grant that permission using ACLs.

To check the permissions, use namei:

namei -l /path/to/directory

See How to check if a user can access a given file? for details.

  • Thanks. Is there a faster way to verify that there is anacestry directory without read or executable permission to anyone than manually ls -l every ancestry directory? – Tim Jun 15 '18 at 12:27
  • Thanks again. Is there a faster way to add read and execution permission bits for all the ancestry directories of a given file, than manually for each ancestry directory? – Tim Jun 15 '18 at 12:37
  • Also is it correct that if I have a symbolic link to a directory which doesn't have read and execution permission for anyone, making the link have read and execution permission for anyone still doesn't allow any one to traverse indirectly into the directory via the link? – Tim Jun 15 '18 at 12:40
  • I don’t think there’s a faster way to set the bits, because the appropriate value can depend on the directories’ ownership. Regarding symbolic links, they don’t carry permissions under Linux; a user’s ability to access a directory pointed to by a symbolic link is entirely determined by the directory’s permissions. – Stephen Kitt Jun 15 '18 at 12:53
  • What do the permission bits of a symlink do? – Tim Jun 15 '18 at 12:59
5

You can, user postgres can't.

sudo -u postgres changes user-id to postgres and then runs the commands. at now user postgres can't access the input file, or even the current directory.

Instead, do it this way:

$ sudo -u postgres pg_restore -C -d dvdrental < dvdrental.tar 

This way the file is opened by your shell under your account before sudo starts and pg_restore then accesses the file content on standard input

  • Thanks. "user postgres can't access the input file, or even the current directory", how or where is this set up? – Tim Jun 15 '18 at 11:29
  • I have added permission bits of the file. The archive file is readable by anyone. – Tim Jun 15 '18 at 11:39
  • seems to be down some rabbit hole of the users home directory, user postgres needs execute permission on all the dirctories in that chain. it's easier to just use redirection instead. – Jasen Jun 16 '18 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.