This is a question about finding an ideal way to deal with a particular situation.
In a folder under my home folder is a group of psql script files that reference each others through \include_relative meta-commands. These scripts sometimes need to be run through psql as a database superuser. Because postgres is peer-authenticated, I must call psql with sudo --user postgres. But since the files are in my home folder, the OS user postgres has no access to them. Now, if there was only a single script file to read, I could simply use a shell redirection, as in sudo -u postgres psql < ~/psql/myscript.psql, but as soon as myscript.psql includes another file, then psql won't find it.
There are various simple and trivial ways of making this work. However, I exclude manually copying/moving/deleting files around every time I need to run a script, and I want to retain the ability to freely move around and rename the source folder and script files without breaking things. Furthermore, this is a mission-critical production server, so interfering with the PostgreSQL service, as in changing the cluster's configuration — for example, creating additional superusers, or changing authentication settings — is out of the question. Ideally, we want this to be transparent to the rest of the server, which excludes solutions involving pre-configured (e.g. ftab, systemd-mount) bind-mount and overlays, or other changes to the server's configuration.
I am looking specifically for a command that makes the script folder available on-the-fly to psql, and only for the duration of the script run. My solution so far is this:
$ sudo mount --bind -o ro,X-mount.mkdir=777 ./psql ~postgres/tmp && \
sudo -iu postgres psql -f ~postgres/tmp/myscript.psql; \
sudo umount ~postgres/tmp && \
sudo rmdir ~postgres/tmp
But still, I find it verbose and I'm trying to improve on it. And I don't like the creation of a tmp folder in ~postgres. Has anybody better ideas?
Edit: Following @larsks' comment below, I realize that my above solution has a privacy flaw in systems where multiple user are given access to the postgres user.
sudo myscriptinstead. I'm not sure anything is going to be more graceful than that. Or seta+xon your home directory (and possibly intervening directories) so that thepostgresuser can read the files in their existing location; it doesn't appear to be a file ownership or permissions problem (you're never callingchownorchmod).postgresrole for your own username, and then granting rights to that role to do whatever it is you need to do? Doing it this way would be roughly analogous to usingsudoto lock down a specific user'srootprivileges instead of giving them the root password.