6

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.

Steve Amerige
  • 1,309
  • 1
  • 12
  • 28

1 Answers1

11

For scripting psql use psql -qAtX:

  • quiet
  • tuples-only
  • unAligned output
  • do not read .psqlrc (X)

To filter columns you must name them in the SELECT list. psql always outputs the full result set it gets from the server. E.g. SELECT status FROM pool_nodes.

Or you can cut to extract ordinal column numbers e.g.

psql -qAtX -c 'whatever' | cut -d '|' -f 1,2-4

(I have no idea how show pool_nodes can produce the output you show here, since SHOW returns a single scalar value...)

To change the delimiter from a pipe | to something else, use -F e.g. -F ','. But be warned, the delimiter is not escaped when it appears in output, this isn't CSV. You might want to consider a tab as a useful option; you have to enter a quoted literal tab to do this. (If doing it in an interactive shell, search for "how to enter literal tab in bash" when you get stuck).

Example showing all the above, given dummy data:

CREATE TABLE dummy_table (
   a integer,
   b integer,
   c text,
   d text
);

INSERT INTO dummy_table
VALUES
(1,1,'chicken','turkey'),
(2,2,'goat','cow'),
(3,3,'mantis','cricket');

query, with single space as the column delimiter (so you'd better not have spaces in your data!):

psql -qAtX -F ' ' -c 'SELECT a, b, d FROM dummy_table'

If for some reason you cannot generate a column-list for SELECT you can instead filter by column-ordinal with cut:

psql -qAtX -F '^' -c 'TABLE dummy_table' | cut -d '^' -f 1-2,4
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • The output using `-qAtX` still includes the vertical bars with all spacing removed. Unfortunately this doesn't answer the question and my OP already shows the `cut` workaround (note: your -f and -c option use is not right). But, thanks for trying! – Steve Amerige Jul 28 '17 at 12:11
  • Uh, brainfart re `cut`. So what do you intend to achieve? Your question is kind of vague in that simply "removing the vertical bars" (pipes) isn't very effective and it's unclear what the underlying goal you're trying to achieve by doing so is. *The output column widths are not fixed and depend on the output data* so `cut`ing by char-column range is not going to work reliably. If you just want to change the delimiter, see the manual page - `-F ,` for example. But note this does not produce CSV - any `,` in the data result is *not escaped*. – Craig Ringer Jul 28 '17 at 14:56
  • Elaborated. Maybe you should explain what your actual underlying goal is? – Craig Ringer Jul 28 '17 at 15:10
  • In the specific case of `show pool_nodes`, each of the data values for the columns will not have spaces. I would rather have the output not have the vertical bars so that the output is just data. I'm not concerned with the fact that the output is of variable length. And, while the `cut` workaround can enable me to select individual columns (with or without adjusting because of vertical bars), the OP is looking for a solution that will enable me to select a specific column from the `show pool_nodes` query (for example, showing just the `status` column) – Steve Amerige Aug 09 '17 at 13:02
  • but ... use `SELECT`, that's what it's for. "show pool_nodes" isn't a valid query, what is the real one? none of this makes sensre – Craig Ringer Aug 10 '17 at 04:02
  • Please see http://www.pgpool.net/docs/latest/en/html/sql-show-pool-nodes.html (I do not know what the underlying implementation is; the show pool_nodes is the only psql command that I know that gets the information shown above) – Steve Amerige Aug 11 '17 at 08:42
  • OK, so you're connected to PgPool, and that's what the query is for. Explains the wackyness and why you cannot just `SELECT` like normal. In which case the answer to your second question is probably "you can't". – Craig Ringer Aug 11 '17 at 09:24