PSQL multiple queries output to file

Hi,

I have a shell script containing multiple PSQL queries for which I want the output to be redirected to a text file.

psql -U postgres -d database -o textfile.txt << EOF
Query1;
Query2;
Query ....;
EOF

When executing the script, queries outputs are directed to textfile.txt, however when script was set in cron , textfile.txt is not being updated.
Does anyone have a different way how I can output query results to a text file.

You probably need to specify a complete file:
-o /path/to/textfile.txt

cron runs as root, then su to the owner of the crontab file. It DOES NOT execute any of the login scripts. So there is a chance that the script is running in some other current directory.

1 Like

Thanks @jim_mcnamara, that works