psql non-select: how to remove formatting and show only certain columns?

Viewed 4591

I'm looking to remove all line drawing characters from:

PGPASSWORD="..." psql -d postgres -h "1.2.3.4" -p 9432 -c 'show pool_nodes' -U owner
 node_id |   hostname    | port | status | lb_weight |  role   
---------+---------------+------+--------+-----------+---------
 0       | 10.20.30.40   | 5432 | 2      | 0.500000  | primary
 1       | 10.20.30.41   | 5432 | 2      | 0.500000  | standby
(2 rows)

Adding the -t option gets rid of the header and footer, but the vertical bars are still present:

PGPASSWORD="..." psql -t -d postgres -h "1.2.3.4" -p 9432 -c 'show pool_nodes' -U owner
 0       | 10.20.30.40   | 5432 | 2      | 0.500000  | primary
 1       | 10.20.30.41   | 5432 | 2      | 0.500000  | standby

Note that this question is specific to show pool_nodes and other similar non-select SQL statements.

My present workaround is to involve the Linux cut command:

<previous command> | cut -d '|' -f 4

The question has two parts:

  1. How using psql only can the vertical bars above be removed?
  2. How using psql only can only a specific column (for example, status) or columns be shown? For example, the result might be just two lines, each showing the number 2.

I'm using psql version psql (PostgreSQL) 9.2.18 on a CentOS 7 server.

1 Answers
Related