psql Cheat Sheet/Run shell command from psql

Execute a shell command from inside psql.

Section: Scripting and Automation

Run shell command from psql

sql
sql
\! date
Explanation

See summary for usage details.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Scripting and Automation
Stop on first error
Exit immediately when a script encounters an error.
OpenIn sheetbashsame section
Pass variable on command line
Inject psql variables from the shell.
OpenIn sheetbashsame section
Echo all commands
Print input lines as they are executed.
OpenIn sheetbashsame section
Echo hidden commands
Show underlying SQL generated by backslash commands.
OpenIn sheetbashsame section
Execute script in one transaction
Wrap the script in a single transaction.
OpenIn sheetbashsame section
Conditional blocks in psql
Use psql conditionals in interactive or scripted sessions.
OpenIn sheetsqlsame section