Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

3
  • I had to do sudo -u postgres -i psql -U .... because sudo -u -i postgres psql -U and sudo -ui postgres psql -U ... didn't work. Commented Jan 12, 2022 at 15:35
  • I don't think that psql wanting to write to $HOME/.psql_history is the cause of this issue. In support of this suspicion I would like to offer the output of sudo -u postgres bash -c 'echo $HOME', which is /var/lib/postgresql; so writing to .psql_history should just work. No, for some reason,psql seems to insist on doing a chdir first to whatever the current working directory is, which for a non-interactive sudo will be the working directory of the invoker; hence the "could not change directory to /home/corey/scripts" output, which is unlikely to be someone's homedir. Commented Dec 15, 2023 at 14:46
  • However, if you do see your own homedirectory printed, then adding a --set-home flag to sudo will definitely print the homedir of the postgres user; but I will bet that, all other things considered equal, sudo --set-home -u postgres psql will still print the "could not change directory to ..." error. Commented Dec 15, 2023 at 14:54