25

I am running a psql batch script and formatting the output as xml. The problem I am running into is that there is a + character in the output. I want to simply use a newline character instead of a plus. I have tried quite a few different arguments and surprised a thorough google search didn't turn up an answer. I am assuming I can use the pset option to change the output eol character but I was unable to get this to work. The script calls query_to_xml on a stored function.

Calling Batch Script

psql -t -q -U user -h database-qa.example.com -f db_test_query.sql -o output.xml DatabaseToUse 

Example Output

<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">        +
                                                                   +
   <order_id>12345</order_id>                                      +
   <status_id>ORDER_COMPLETED</status_id>                          +
   <customer_id>9999</customer_id>                                 +
   <company_name>ExampleComany</company_name>                      +
   <main_contact_name>user</main_contact_name>                     +
   <email_address>[email protected]</email_address>                +
   <country_code xsi:nil="true"/>                                  +
   <local_number>1112223333</local_number>                         +
   <address1>1009 Customer Ln</address1>                           +
   <address2></address2>                                           +
   <city>Houston</city>                                            +
   <state_province_geo_id>TX</state_province_geo_id>               +
   <postal_code>77380</postal_code>                                +
   ...

1 Answer 1

52

Use unaligned output mode.

psql -qAtX ....

will suppress informational output (quiet), use unaligned mode, output tuples only, suppress psqlrc reading.

Sign up to request clarification or add additional context in comments.

5 Comments

Note: this may not work anymore with PG client 9.6, because .psqlrc now is used with -c too (postgresql.org/docs/9.6/static/release-9-6.html). It seems that not only it is taken into account, but it overrides command line parameters too
@piro Right, on 9.6 you need -X too.
Isn't it a bit daft the fact .psqlrc overrides command line parameters? Let alone all the scripts I have to fix because they work differently for me and for others (but who needs consistency these days: random results for everyone is more fun).
Yeah, I think it's totally daft. Bugreport-worthy frankly. Though good luck getting it changed due to BC.
FWIW, reported.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.