Nadjib
Nadjib

Reputation: 361

Display each column value on separate line for SELECT CLAUSE in PSQL

Using psql on my shell, I am trying to display the output of a SELECT CLAUSE in a specific way. I want each column value of my rows to be displayed on its own line.

Right now, my output looks like this (the display I don’t want):

Expanded display is on.
idOne   nameOne   createdAtOne
idTwo   nameTwo   createdAtTwo

But I want it to look like this

Expanded display is on.
idOne
nameOne
createdAtOne
idTwo
nameTwo
createdAtTwo

My command line is the following

psql -h hostName -d databaseName -U userName -p 5432 -c '\x on' -c "
COPY (
SELECT
  e.id,
  e.name,
  e.created_at
FROM entity_name e
) TO STDOUT
;
"

I would like each value of a column in a row of my table to be displayed on its own separate line.

But although I have used the -c ‘\x on’ in my command line, and even if the sentence Expanded display is on is displayed, my output is still displayed the first way and not in an expanded way.

How can I do this? What am I missing?

My psql version on my remote server is psql (PostgreSQL) 9.6.6 and I am using MAC.

(ps: if it possible, I would even like my output to look like this

id idOne
name nameOne
created_at createdAtOne
id idTwo
name nameTwo
created_at createdAtTwo

Upvotes: 2

Views: 2714

Answers (1)

Vao Tsun
Vao Tsun

Reputation: 51529

https://www.postgresql.org/docs/current/static/app-psql.html#APP-PSQL-META-COMMANDS

This command sets options affecting the output of query result tables

in other words it affects only tables displayed, while COPY to STDOUT shows you the output of resulting generated tsc or csv. thus \x does not affect it.

Also from your query - I see no need in COPY - you just copy from query to stdout - thus omitting copy and just running

SELECT
  e.id,
  e.name,
  e.created_at
FROM entity_name e

in expanded display will give expected result. You will have to grep -v 'RECORD' though

Upvotes: 3

Related Questions