PostgreSQL – psql ‘-c’ or ‘-f’ Option Not Terminating Properly

postgresqlpsql

When I run a command such psql -d database -c 'select * from table' or psql -d database -f sqlcommands psql does not terminate cleanly. Interestingly piping the the contents of the file instead of using the -foption eg, psql -d database < sqlcommands doesn't display this behaviour.

I need to press the q key as required by less or man for the execution to finish and return to the command prompt?

Is that the standard behaviour? Are the some escapes required that will allow it to terminate cleanly?

Best Answer

I need to press the q key as required by less or man for the execution to finish and return to the command prompt?

Sounds like your terminal's pager is taking over the output: you can tell psql not to send its output to the pager when you're using the -c 'select... ' invocation via:

psql --pset pager=off -d database -c 'SELECT * from TABLE'

You may also put the line pset pager OFF in the file ~/.psqlrc, however it seems ~/.psqlrc directives are honored only in interactive mode, not for commands passed in via -c '...'. More great tips for non-interactive psql usage from Peter Eisentraut.