Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove eol (plus sign) character in postgres batch output

Tags:

sql

postgresql

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>                                +
   ...
like image 451
Chris Hinshaw Avatar asked Mar 17 '14 15:03

Chris Hinshaw


People also ask

How do I ignore special characters in PostgreSQL?

Using function regexp_replace() Using regexp_replace we can remove the special characters from the string or columns.

How do you handle special characters in PostgreSQL?

Special character symbols are characters with a pre-defined syntactic meaning in PostgreSQL. They are typically disallowed from being used in identifier names for this reason, though as mentioned in the section on quoted identifiers, this restriction can usually be worked around with quotes if need be.

How do I escape reserved words in PostgreSQL?

Simply enclose year in double quotes to stop it being interpreted as a keyword: INSERT INTO table (id, name, "year") VALUES ( ... );

What does ~* mean in PostgreSQL?

The tilde operator returns true or false depending on whether or not a regular expression can match a string or a part thereof. ~ (Matches regular expression, case sensitive) ~* (Matches regular expression, case insensitive)


1 Answers

Use unaligned output mode.

psql -qAtX ....

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

like image 77
Craig Ringer Avatar answered Oct 07 '22 16:10

Craig Ringer