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> +
...
Using function regexp_replace() Using regexp_replace we can remove the special characters from the string or columns.
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.
Simply enclose year in double quotes to stop it being interpreted as a keyword: INSERT INTO table (id, name, "year") VALUES ( ... );
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)
Use unaligned output mode.
psql -qAtX ....
will suppress informational output (quiet), use unaligned mode, output tuples only, suppress psqlrc reading.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With