Skip to main content
indent code to match list
Source Link
muru
  • 78.3k
  • 16
  • 214
  • 320
  1. Add -i to sudo command to use login shell which changes the working directory to the home directory of the postgres user before executing the psql command (postgres user naturally has the x permission to its own home directory):

    Add -i to sudo command to use login shell which changes the working directory to the home directory of the postgres user before executing the psql command (postgres user naturally has the x permission to its own home directory):

    sudo -i -u postgres psql < setup_dev_db.sql 
  2. Give postgres user x permission to the current working directory by adding it to the group owning the working directory:

    # Get group owning the current directory stat -c "%G" . # Add postgres user to that group sudo usermod -aG <group above here> postgres 
  3. Give postgres user x permission to the current working directory by giving x permission to "others" to the working directory. This can be handy e.g., in single user VM-based systems meant for development, such as WSL2 where simple chmod o+x /home/$USER command will fix the error.

  4. Run the psql using current user which of course have permissions to the current working directory.

  5. Before executing the command, change working directory to a directory where postgres user has x permission, e.g., cd /tmp.

sudo -i -u postgres psql < setup_dev_db.sql 
  1. Give postgres user x permission to the current working directory by adding it to the group owning the working directory:
# Get group owning the current directory stat -c "%G" . # Add postgres user to that group sudo usermod -aG <group above here> postgres 
  1. Give postgres user x permission to the current working directory by giving x permission to "others" to the working directory. This can be handy e.g., in single user VM-based systems meant for development, such as WSL2 where simple chmod o+x /home/$USER command will fix the error.

  2. Run the psql using current user which of course have permissions to the current working directory.

  3. Before executing the command, change working directory to a directory where postgres user has x permission, e.g., cd /tmp.

  1. Add -i to sudo command to use login shell which changes the working directory to the home directory of the postgres user before executing the psql command (postgres user naturally has the x permission to its own home directory):
sudo -i -u postgres psql < setup_dev_db.sql 
  1. Give postgres user x permission to the current working directory by adding it to the group owning the working directory:
# Get group owning the current directory stat -c "%G" . # Add postgres user to that group sudo usermod -aG <group above here> postgres 
  1. Give postgres user x permission to the current working directory by giving x permission to "others" to the working directory. This can be handy e.g., in single user VM-based systems meant for development, such as WSL2 where simple chmod o+x /home/$USER command will fix the error.

  2. Run the psql using current user which of course have permissions to the current working directory.

  3. Before executing the command, change working directory to a directory where postgres user has x permission, e.g., cd /tmp.

  1. Add -i to sudo command to use login shell which changes the working directory to the home directory of the postgres user before executing the psql command (postgres user naturally has the x permission to its own home directory):

    sudo -i -u postgres psql < setup_dev_db.sql 
  2. Give postgres user x permission to the current working directory by adding it to the group owning the working directory:

    # Get group owning the current directory stat -c "%G" . # Add postgres user to that group sudo usermod -aG <group above here> postgres 
  3. Give postgres user x permission to the current working directory by giving x permission to "others" to the working directory. This can be handy e.g., in single user VM-based systems meant for development, such as WSL2 where simple chmod o+x /home/$USER command will fix the error.

  4. Run the psql using current user which of course have permissions to the current working directory.

  5. Before executing the command, change working directory to a directory where postgres user has x permission, e.g., cd /tmp.

Add solution 5
Source Link

TL;DR: There is discussion about this issue in Postgres mailing list and it has been fixed in PostgreSQL 16.

Reason for the error:
While @lambart's solutions are correct, the reason for the error is not since it has nothing to do with .psql_history file. The

could not change directory to "/home/corey/scripts": Permission denied 

error is coming from resolve_symlinks() function which resolves symlinks using chdir and finally tries to chdir back to the original directory which fails if the user executing psql does not have permissions to do so. The resolve_symlinks() call comes from find_my_exec() which in turn is called by set_pglocale_pgservice() (which sets PGSYSCONFDIR and PGLOCALEDIR).

Solutions for older versions:

For versions prior 16, the error is fixed by giving the user executing psql command x permission to the working directory where the command is issued. This can be done in several ways:

  1. Add -i to sudo command to use login shell which changes the working directory to the home directory of the postgres user before executing the psql command (postgres user naturally has the x permission to its own home directory):
sudo -i -u postgres psql < setup_dev_db.sql 
  1. Give postgres user x permission to the current working directory by adding it to the group owning the working directory:
# Get group owning the current directory stat -c "%G" . # Add postgres user to that group sudo usermod -aG <group above here> postgres 
  1. Give postgres user x permission to the current working directory by giving x permission to "others" to the working directory. This can be handy e.g., in single user VM-based systems meant for development, such as WSL2 where simple chmod o+x /home/$USER command will fix the error.

  2. Run the psql using current user which of course have permissions to the current working directory.

  3. Before executing the command, change working directory to a directory where postgres user has x permission, e.g., cd /tmp.

TL;DR: There is discussion about this issue in Postgres mailing list and it has been fixed in PostgreSQL 16.

Reason for the error:
While @lambart's solutions are correct, the reason for the error is not since it has nothing to do with .psql_history file. The

could not change directory to "/home/corey/scripts": Permission denied 

error is coming from resolve_symlinks() function which resolves symlinks using chdir and finally tries to chdir back to the original directory which fails if the user executing psql does not have permissions to do so. The resolve_symlinks() call comes from find_my_exec() which in turn is called by set_pglocale_pgservice() (which sets PGSYSCONFDIR and PGLOCALEDIR).

Solutions for older versions:

For versions prior 16, the error is fixed by giving the user executing psql command x permission to the working directory where the command is issued. This can be done in several ways:

  1. Add -i to sudo command to use login shell which changes the working directory to the home directory of the postgres user before executing the psql command (postgres user naturally has the x permission to its own home directory):
sudo -i -u postgres psql < setup_dev_db.sql 
  1. Give postgres user x permission to the current working directory by adding it to the group owning the working directory:
# Get group owning the current directory stat -c "%G" . # Add postgres user to that group sudo usermod -aG <group above here> postgres 
  1. Give postgres user x permission to the current working directory by giving x permission to "others" to the working directory. This can be handy e.g., in single user VM-based systems meant for development, such as WSL2 where simple chmod o+x /home/$USER command will fix the error.

  2. Run the psql using current user which of course have permissions to the current working directory.

TL;DR: There is discussion about this issue in Postgres mailing list and it has been fixed in PostgreSQL 16.

Reason for the error:
While @lambart's solutions are correct, the reason for the error is not since it has nothing to do with .psql_history file. The

could not change directory to "/home/corey/scripts": Permission denied 

error is coming from resolve_symlinks() function which resolves symlinks using chdir and finally tries to chdir back to the original directory which fails if the user executing psql does not have permissions to do so. The resolve_symlinks() call comes from find_my_exec() which in turn is called by set_pglocale_pgservice() (which sets PGSYSCONFDIR and PGLOCALEDIR).

Solutions for older versions:

For versions prior 16, the error is fixed by giving the user executing psql command x permission to the working directory where the command is issued. This can be done in several ways:

  1. Add -i to sudo command to use login shell which changes the working directory to the home directory of the postgres user before executing the psql command (postgres user naturally has the x permission to its own home directory):
sudo -i -u postgres psql < setup_dev_db.sql 
  1. Give postgres user x permission to the current working directory by adding it to the group owning the working directory:
# Get group owning the current directory stat -c "%G" . # Add postgres user to that group sudo usermod -aG <group above here> postgres 
  1. Give postgres user x permission to the current working directory by giving x permission to "others" to the working directory. This can be handy e.g., in single user VM-based systems meant for development, such as WSL2 where simple chmod o+x /home/$USER command will fix the error.

  2. Run the psql using current user which of course have permissions to the current working directory.

  3. Before executing the command, change working directory to a directory where postgres user has x permission, e.g., cd /tmp.

Source Link

TL;DR: There is discussion about this issue in Postgres mailing list and it has been fixed in PostgreSQL 16.

Reason for the error:
While @lambart's solutions are correct, the reason for the error is not since it has nothing to do with .psql_history file. The

could not change directory to "/home/corey/scripts": Permission denied 

error is coming from resolve_symlinks() function which resolves symlinks using chdir and finally tries to chdir back to the original directory which fails if the user executing psql does not have permissions to do so. The resolve_symlinks() call comes from find_my_exec() which in turn is called by set_pglocale_pgservice() (which sets PGSYSCONFDIR and PGLOCALEDIR).

Solutions for older versions:

For versions prior 16, the error is fixed by giving the user executing psql command x permission to the working directory where the command is issued. This can be done in several ways:

  1. Add -i to sudo command to use login shell which changes the working directory to the home directory of the postgres user before executing the psql command (postgres user naturally has the x permission to its own home directory):
sudo -i -u postgres psql < setup_dev_db.sql 
  1. Give postgres user x permission to the current working directory by adding it to the group owning the working directory:
# Get group owning the current directory stat -c "%G" . # Add postgres user to that group sudo usermod -aG <group above here> postgres 
  1. Give postgres user x permission to the current working directory by giving x permission to "others" to the working directory. This can be handy e.g., in single user VM-based systems meant for development, such as WSL2 where simple chmod o+x /home/$USER command will fix the error.

  2. Run the psql using current user which of course have permissions to the current working directory.