I have the following problem: On every machine running Postgresql there is a special user postgres. This user has administrative access to the database server.
Now I want to write a Bash script that executes a database command with psql as user postgres (psql shall execute as user postgres, not the script). So far, that wouldn't be a problem: I could just run the script as user postgres.
However, I want to write the output of psql to a file in a directory where postgres has no write access.
How can I do that?
I thought about changing EUIDs in the script itself, however:
- I couldn't find a way to change the EUID in a Bash script
- How can I change the EUID when using something like
psql -U postgres -c "<command>" > file
?
Best Answer
Use a subshell:
(su -c 'psql -U postgres -c "<command>"' postgres) > file
Inside the subshell you can drop permissions to do your work, but output is redirected to your original shell which still has your original permissions.