How to hide result set decoration in Psql output
To quickly strip away result decoration in psql
, use:
To return to the default presentation, execute:
To output query results to a file sans extra dressing, use \o
:
Advanced customization
Using COPY for no-frills output
The COPY
command in PostgreSQL, when used with -t
or --tuples-only
, can output results unadorned with headers or footers:
Ensure the path is absolute to prevent any directory confusion. Also, remember, COPY
command requires superuser privileges or being the table owner.
The -c and --output psql ninjas
For situations when a specific query should be executed from the command line, -c
is your go-to option:
Using the --output
option will send your query results to a file, all without inviting "extra formatting" to your party.
The -A and -F combo for precision strikes
Use the -A
option for an unaligned output and -F
to specify a field separator. A combo move like the below makes CSV-style output your ally:
Scripting your way through
When built-in options don't meet your requirements, let bash scripting step in. Use it to parse and format the psql output:
Getting column names on the fly
For times when you need to retrieve column names dynamically, use information_schema.columns
:
Best practices
Automate with .psqlrc
Make your life easier and automate settings in your .psqlrc
file. For example, to always hide row count:
Learning from examples
Understanding in-depth control over psql can be achieved by diligently studying command line examples. Here's an example that shows querying, output file, and formatting in one line:
Knowing your tools well
Spend some quality time with PostgreSQL documentation to unlock hidden features and grasp best practices.
Was this article helpful?