I am currently outputting the results of an sql query to a CSV file with a header using the copy command in Postgresql. What I want to do is add a line break between the header and the start of the CSV data so that it is more readable.
Is there an easy way to do this by just altering my query? Thanks for your help!
Actually, it is possible, although it is a bit of a hack:
Rename the final column of the output using an AS clause in the query to add a newline character to the end of the column. You have to be sure to double-quote the column name in order to preserve the non-standard character, and you can’t use that column in the FORCE QUOTE clause of COPY.
I was able to get it to work as you requested with this query:
The first three lines of output on my system look like this:
Please note that the “^L” in this output is my actually using the key combination in a Linux console in the PostgreSQL 9.2 psql client. Trying to use the string “^L” doesn’t work. Also, this doesn’t work either, although it gets you closer (note the line break):
This ends up with the first two lines looking like this:
Which isn’t quite correct.
You’ll just need to tinker in your specific environment on how to get that newline character added but hopefully that’s enough info here to start.