How to set default display mode in psql

后端 未结 4 799
长发绾君心
长发绾君心 2021-02-05 04:27

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

相关标签:
4条回答
  • 2021-02-05 04:31

    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.

    0 讨论(0)
  • 2021-02-05 04:34

    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.

    0 讨论(0)
  • 2021-02-05 04:36

    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
    

    within the psql shell:

    \x
    
    0 讨论(0)
  • 2021-02-05 04:50

    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
    
    0 讨论(0)
提交回复
热议问题