<?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>Joys and rants of a Python programmer &#187; postgresql</title>
	<atom:link href="http://blog.pow.lt/tag/postgresql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.pow.lt</link>
	<description>Pow! Wham, bam, kapow!</description>
	<lastBuildDate>Wed, 02 Dec 2009 16:04:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Cleaning up postgresql database</title>
		<link>http://blog.pow.lt/2009/05/30/cleaning-up-postgresql-database/</link>
		<comments>http://blog.pow.lt/2009/05/30/cleaning-up-postgresql-database/#comments</comments>
		<pubDate>Sat, 30 May 2009 01:23:04 +0000</pubDate>
		<dc:creator>Ignas Mikalajūnas</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[postgresql]]></category>

		<guid isPermaLink="false">http://blog.pow.lt/?p=4</guid>
		<description><![CDATA[Sometimes when writing tests or copying production database into testing servers you need to get your database back to a clean state, and you just don&#8217;t want to &#8220;DROP DATABASE&#8220;.

I can&#8217;t even do &#8220;DROP DATABASE&#8221; during my test run, because my testing user does not have the rights to do DROP/CREATE databases. So I am [...]]]></description>
			<content:encoded><![CDATA[Sometimes when writing tests or copying production database into testing servers you need to get your database back to a clean state, and you just don&#8217;t want to &#8220;<strong>DROP DATABASE</strong>&#8220;.

I can&#8217;t even do &#8220;<strong>DROP DATABASE</strong>&#8221; during my test run, because my testing user does not have the rights to do <strong>DROP</strong>/<strong>CREATE</strong> databases. So I am abusing^W taking advantage of the fact that by default all the tables, procedures, triggers and pretty much everything you add to the database are added to the default schema. As I am not adding any new schemas, I can just &#8220;<strong>DROP SCHEMA PUBLIC CASCADE</strong>&#8221; which gets rid of the default schema, and everything that is related to it (don&#8217;t forget to add it back with &#8220;<strong>CREATE SCHEMA PUBLIC</strong>&#8221; so you could add tables to the database).

I am not sure whether this operation can have any dangerous side effects, but for a quick and 100% reliable teardown &#8211; it works perfectly. And I am not really worrying about our testing database&#8230;

I tried to use the &#8220;-c&#8221; flag which you can pass to pg_restore. Running:
<pre><code>pg_dump production -Fc -O | pg_restore -d testing -c
</code></pre>
works fine most of the time. The command will delete all the tables and all the triggers that were present in your <strong>production</strong> database from your <strong>testing</strong> database, and &#8220;<strong>DROP SCHEMA PUBLIC</strong>&#8221; (notice the missing <strong>CASCADE</strong>). But it will fail if you had any other tables in your <strong>testing</strong> database. As I am going to be testing my evolution scripts on the testing database a lot, it will have additional tables and triggers most of the time and I really don&#8217;t want that interfering with my tests.<a href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fblog.pow.lt%2F2009%2F05%2F30%2Fcleaning-up-postgresql-database%2F&amp;linkname=Cleaning%20up%20postgresql%20database" title="Digg" rel="nofollow" target="_blank"><img src="http://blog.pow.lt/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a> <a href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fblog.pow.lt%2F2009%2F05%2F30%2Fcleaning-up-postgresql-database%2F&amp;linkname=Cleaning%20up%20postgresql%20database" title="Reddit" rel="nofollow" target="_blank"><img src="http://blog.pow.lt/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fblog.pow.lt%2F2009%2F05%2F30%2Fcleaning-up-postgresql-database%2F&amp;linkname=Cleaning%20up%20postgresql%20database" title="Delicious" rel="nofollow" target="_blank"><img src="http://blog.pow.lt/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fblog.pow.lt%2F2009%2F05%2F30%2Fcleaning-up-postgresql-database%2F&amp;linkname=Cleaning%20up%20postgresql%20database" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://blog.pow.lt/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fblog.pow.lt%2F2009%2F05%2F30%2Fcleaning-up-postgresql-database%2F&amp;linkname=Cleaning%20up%20postgresql%20database"><img src="http://blog.pow.lt/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://blog.pow.lt/2009/05/30/cleaning-up-postgresql-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
