psql shell command execution with \! psql shell command execution with \! postgresql postgresql

psql shell command execution with \!


Using \! with \o..

You can combine the two like this,

  • \! echo "SELECT 1;" > bar (runs the command echo "SELECT 1;" redirects output of SELECT 1; to bar)
  • \o bar (runs the commands in bar)


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.