psql -c 'SELECT version();'

Category: Databases & SQL

psql -c 'SELECT version();'

Run one SQL statement and exit

The -c flag executes a single statement and returns to the shell, which makes it ideal for scripts. Wrap the SQL in single quotes so the shell does not interpret special characters. Related: psql -f to run a whole file.
Looking for more? Search all 7,657 commands — works offline, in English or Spanish, and fixes typos.