psql shell command execution with \!
Using \!
with \o
..
You can combine the two like this,
\! echo "SELECT 1;" > bar
(runs the commandecho "SELECT 1;"
redirects output ofSELECT 1;
tobar
)\o bar
(runs the commands inbar
)
Seems to be impossible for \!
,
but one can set a variable from external command output,
testdb=> \set content `cat my_file.txt`
and then using this var in sql expression like this:
testdb=> INSERT INTO my_table VALUES (:'content');
You can use \o <filename>
option to specify the output file or use COPY
command to solve your problem.