<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Darren O&#039;Neill &#187; CSV</title>
	<atom:link href="http://www.darrenoneill.co.uk/tag/csv/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.darrenoneill.co.uk</link>
	<description></description>
	<lastBuildDate>Fri, 09 Dec 2011 17:27:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Exporting PostgreSQL result set to CSV</title>
		<link>http://www.darrenoneill.co.uk/exporting-postgresql-result-set-to-csv/</link>
		<comments>http://www.darrenoneill.co.uk/exporting-postgresql-result-set-to-csv/#comments</comments>
		<pubDate>Sat, 15 Aug 2009 19:40:41 +0000</pubDate>
		<dc:creator>Darren</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[CSV]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[RegEx]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://darrenoneill.co.uk/?p=337</guid>
		<description><![CDATA[I probably use PostgreSQL just as much as MySQL these days due to client requirements and legacy applications I need to support. I don't mind this; I actually like PostgreSQL and used it extensively at university.]]></description>
			<content:encoded><![CDATA[<p>I probably use PostgreSQL just as much as MySQL these days due to client requirements and legacy applications I need to support. I don&#8217;t mind this; I actually like PostgreSQL and used it extensively at university. One problem I do have with it though is the fact there is no real support for exporting data in CSV format. In MySQL you can do something like:</p>
<p><code>SELECT * INTO OUTFILE '/path/to/file/extract.csv'<br />
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'<br />
LINES TERMINATED BY '\n'<br />
FROM `users`<br />
WHERE first_name='John';</code></p>
<p>This works a treat and will generate a nicely formatted file for you which you can open in Excel.</p>
<p>I have found the best way to replicate this functionality in PostgreSQL is to firstly run the \a command in the console to get unaligned results. If you don&#8217;t, running the PostgreSQL equivalent query:</p>
<p><code>SELECT * FROM "users" WHERE first_name='John' \o /path/to/file/extract.csv</code></p>
<p>will generate this:</p>
<p><code> first_name | last_name<br />
------------+-----------<br />
John       | Smith<br />
John       | Brown<br />
</code></p>
<p>I then either open the unaligned file in Komodo  and run a regular expression to replace all the pipe characters with commas or open the file in Excel and set the delimiter to be the pipe character.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.darrenoneill.co.uk/exporting-postgresql-result-set-to-csv/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

