Almost every psql query comes out an illegible mess if not preceded by display configuration commands such as \\x auto
. Typing this manually for each psql instance
Use .psqlrc to set defaults.
For the specific case of \x
, newer psql
versions (9.3, I think, but it might just be the 9.4 pre-release) can automatically switch to expanded output mode when the rows are too wide to fit on a line. From \?
:
\x [on|off|auto] toggle expanded output (currently off)
So I suggest putting \x auto
in your .psqlrc
, rather than forcing it to on
.
There is an option of -x but it does not do the job for me as I am using COPY to send output to a file. Without copy "-x" works well!
So here is what I have done to read:
# psql -U postgres -o /tmp/output_file_name.txt <<EOF
\x
SELECT * FROM pg_stat_activity;
EOF
Expanded display is on.
One can morph the above to whatever he/she needs to do. One can also do the same with an input file.
It should also be noted that the psql
command line option -x
or --expanded
is the same as using \x
from within.
psql
command-line options:-P expanded=auto
--pset expanded=auto
-x
--expanded
psql
shell:\x
For me I add -q to turn that off for my psql command e.g.
psql postgresql://u:p@h:5432/db -q -c 'select 1'
# connection string turn it off query