Search code examples
postgresqlshellpsql

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.


Solution

  • Using \! with \i..

    You can combine the two like this,

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