I want to export some table to xml file:
\o output.xml
SELECT table_to_xml('modified_records', TRUE, TRUE, '');
However, I'm struggling with psql formatting. The best I managed to get is:
\t on
\x on
\pset format unaligned
\o modified_records.xml
SELECT table_to_xml('modified_records', TRUE, TRUE, '');
\o
Which results in
table_to_xml|<modified_records xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tag1>val1</tag1>
<tag2>val2</tag2>
</modified_records>
<modified_records xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tag1>val1</tag1>
<tag2>val2</tag2>
</modified_records>
<modified_records xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tag1>val1</tag1>
<tag2>val2</tag2>
</modified_records>
So I still have to remove table_to_xml|
. Which is not hard:
cat modified_records.xml | sed '1s/^table_to_xml|//'
# or even in psql:
\o | sed '1s/^table_to_xml|//' > modified_records.xml
but feels like fighting with Postgres.
Is there a way to dump just the value?
(there are plenty of options for \pset
, but I can't find its docs)
Best Answer
The psql command \? shows the option to turn off expanded formatting:
Typically it will show table data in one record per line if its "off" or else each field in its own line when switched on, e.g.:
When its switched on:
With xml output, since everything is in one stream, it shows the record label before printing out the xml. The difference between /x on and /x off is as follows:
With
/x off
the output is:So before writing to the file you should change the commands issued as follows: