Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

psql shell command execution with \!

Is there any way to capture shell output (in a psql variable) and/or the exit code of running a shell command from inside psql using \!? The documentation provides no details.

like image 971
Sim Avatar asked Aug 24 '15 19:08

Sim


2 Answers

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)
like image 95
NO WAR WITH RUSSIA Avatar answered Oct 02 '22 09:10

NO WAR WITH RUSSIA


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');
like image 31
Pavel T Avatar answered Oct 02 '22 08:10

Pavel T