<?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>Mad Python &#187; unix</title>
	<atom:link href="http://blog.madpython.com/category/tech/unix/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.madpython.com</link>
	<description>Watch out.. he's angry</description>
	<lastBuildDate>Thu, 04 Nov 2010 14:19:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Extending PostgreSQL with Python</title>
		<link>http://blog.madpython.com/2009/03/07/extending-postgresql-with-python/</link>
		<comments>http://blog.madpython.com/2009/03/07/extending-postgresql-with-python/#comments</comments>
		<pubDate>Sat, 07 Mar 2009 22:56:31 +0000</pubDate>
		<dc:creator>Xavier</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[unix]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://blog.madpython.com/?p=40</guid>
		<description><![CDATA[One of the features I enjoy the most about PostgreSQL is the ability to write stored procedures in C, Perl, TCL, PgSQL, and yes&#8230; obviously also in Python. I&#8217;ve been using this feature since 7.4, so any recent version of &#8230; <a href="http://blog.madpython.com/2009/03/07/extending-postgresql-with-python/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://blog.madpython.com/2009/03/06/fixing-custom-sequences-in-postgresql/' rel='bookmark' title='Fixing custom sequences in PostgreSQL'>Fixing custom sequences in PostgreSQL</a></li>
<li><a href='http://blog.madpython.com/2009/03/01/ahh-those-cool-little-cli-tools/' rel='bookmark' title='Ahh those cool little CLI tools&#8230;'>Ahh those cool little CLI tools&#8230;</a></li>
<li><a href='http://blog.madpython.com/2009/03/02/apache2-shortcuts/' rel='bookmark' title='Apache2 shortcuts'>Apache2 shortcuts</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>One of the features I enjoy the most about PostgreSQL is the ability to write stored procedures in C, Perl, TCL, PgSQL, and yes&#8230; obviously also in Python. I&#8217;ve been using this feature since 7.4, so any recent version of PostgreSQL is pretty much guaranteed to support it, but you&#8217;ll need to have the pl/python procedural language contrib module installed. Once it&#8217;s installed, you can activate it for your current database using the following query:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">PROCEDURAL</span> <span style="color: #993333; font-weight: bold;">LANGUAGE</span> plpythonu;</pre></div></div>

<p>Once the language bindings have been activated, you can start writing your stored procedures in python, however you should really read up on the following subjects first:</p>
<ul>
<li><a href="http://www.postgresql.org/docs/8.3/interactive/sql-createfunction.html" onclick="return TrackClick('http%3A%2F%2Fwww.postgresql.org%2Fdocs%2F8.3%2Finteractive%2Fsql-createfunction.html','CREATE+FUNCTION+statement+syntax')" target="_blank">CREATE FUNCTION statement syntax</a></li>
<li><a href="http://www.postgresql.org/docs/8.3/interactive/plpython-funcs.html" onclick="return TrackClick('http%3A%2F%2Fwww.postgresql.org%2Fdocs%2F8.3%2Finteractive%2Fplpython-funcs.html','PL%2FPython+Implementation+Documentation')" target="_blank">PL/Python Implementation Documentation</a></li>
</ul>
<p>As an example, I&#8217;ve written a little <acronym title="Stored Procedure">SP</acronym> in PL/Python to provide support for <acronym title="Perl-Compatible Regular Expressions">PCRE</acronym> since the stock distribution of PostgreSQL only supports LIKE/SIMILAR, and POSIX Style Regular Expressions.</p>
<p>Let&#8217;s create our Python language binding, and create a standard text storage table:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- Activate PL/Python</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">PROCEDURAL</span> <span style="color: #993333; font-weight: bold;">LANGUAGE</span> plpythonu;
&nbsp;
<span style="color: #808080; font-style: italic;">-- Create a plain text-storage table</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> text_storage
<span style="color: #66cc66;">&#40;</span>
  id serial <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  payload <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">VARYING</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">CONSTRAINT</span> text_storage_pkey <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #66cc66;">&#40;</span>OIDS<span style="color: #66cc66;">=</span><span style="color: #993333; font-weight: bold;">FALSE</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> text_storage OWNER <span style="color: #993333; font-weight: bold;">TO</span> xavier;
&nbsp;
<span style="color: #808080; font-style: italic;">-- Let's throw in an index on the payload field for good measure</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">INDEX</span> txt_payload_idx
  <span style="color: #993333; font-weight: bold;">ON</span> text_storage
  <span style="color: #993333; font-weight: bold;">USING</span> btree
  <span style="color: #66cc66;">&#40;</span>payload<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Let&#8217;s now populate our table with some junk data:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> text_storage <span style="color: #66cc66;">&#40;</span>payload<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'hello, world'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> text_storage <span style="color: #66cc66;">&#40;</span>payload<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'the quick brown fox, blah blah blah'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> text_storage <span style="color: #66cc66;">&#40;</span>payload<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'PCREs in Postgres'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> text_storage <span style="color: #66cc66;">&#40;</span>payload<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'All hail Python!'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> text_storage <span style="color: #66cc66;">&#40;</span>payload<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Hello, test data!'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> text_storage <span style="color: #66cc66;">&#40;</span>payload<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Python would like to say Hello!'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>And now we can go ahead and create our Python SP itself:</p>

<div class="wp_syntax"><div class="code"><pre class="plsql" style="font-family:monospace;"><span style="color: #00F;">CREATE</span> <span style="color: #00F;">OR</span> <span style="color: #000;">REPLACE</span> <span style="color: #00F;">FUNCTION</span> pcre<span style="color: #00F;">&#40;</span>text<span style="color: #00F;">,</span> text<span style="color: #00F;">&#41;</span>
  RETURNS <span style="color: #00F;">INTEGER</span> <span style="color: #00F;">AS</span>
$BODY$import re
&nbsp;
regex  <span style="color: #00F;">=</span> args<span style="color: #00F;">&#91;</span><span style="color: #800;">0</span><span style="color: #00F;">&#93;</span>
in_str <span style="color: #00F;">=</span> args<span style="color: #00F;">&#91;</span><span style="color: #800;">1</span><span style="color: #00F;">&#93;</span>
&nbsp;
compiled <span style="color: #00F;">=</span> re<span style="color: #00F;">.</span>compile<span style="color: #00F;">&#40;</span>regex<span style="color: #00F;">&#41;</span>
&nbsp;
<span style="color: #00F;">IF</span> compiled<span style="color: #00F;">.</span>search<span style="color: #00F;">&#40;</span>in_str<span style="color: #00F;">&#41;</span><span style="color: #00F;">:</span>
	<span style="color: #00F;">RETURN</span> <span style="color: #800;">1</span>
<span style="color: #00F;">ELSE</span><span style="color: #00F;">:</span>
	<span style="color: #00F;">RETURN</span> <span style="color: #800;">0</span>$BODY$
  LANGUAGE <span style="color: #F00;">'plpythonu'</span> VOLATILE
  COST <span style="color: #800;">100</span><span style="color: #00F;">;</span></pre></div></div>

<p>As you can see, our PCRE matching system is extremely simple, yet pretty powerful. We import Python&#8217;s built-in <strong>re</strong> module, compile the specified regex argument, then attempt to match it against the other argument. Here&#8217;s a usage example on our test table:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> payload <span style="color: #993333; font-weight: bold;">FROM</span> text_storage <span style="color: #993333; font-weight: bold;">WHERE</span> pcre<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'[H|h]ello'</span><span style="color: #66cc66;">,</span> payload<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>;
 id <span style="color: #66cc66;">|</span>             payload             
<span style="color: #808080; font-style: italic;">----+---------------------------------</span>
  <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> hello<span style="color: #66cc66;">,</span> world
  <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">|</span> Hello<span style="color: #66cc66;">,</span> test <span style="color: #993333; font-weight: bold;">DATA</span>!
  <span style="color: #cc66cc;">6</span> <span style="color: #66cc66;">|</span> Python would <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #993333; font-weight: bold;">TO</span> say Hello!
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span> <span style="color: #993333; font-weight: bold;">ROWS</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>As always, feel free to suggest any improvements.</p>
<p><a class="a2a_button_reddit" href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F07%2Fextending-postgresql-with-python%2F&amp;linkname=Extending%20PostgreSQL%20with%20Python" title="Reddit" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F07%2Fextending-postgresql-with-python%2F&amp;linkname=Extending%20PostgreSQL%20with%20Python" title="Delicious" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F07%2Fextending-postgresql-with-python%2F&amp;linkname=Extending%20PostgreSQL%20with%20Python" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F07%2Fextending-postgresql-with-python%2F&amp;linkname=Extending%20PostgreSQL%20with%20Python" title="Facebook" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F07%2Fextending-postgresql-with-python%2F&amp;linkname=Extending%20PostgreSQL%20with%20Python" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a><a class="a2a_button_linkedin" href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F07%2Fextending-postgresql-with-python%2F&amp;linkname=Extending%20PostgreSQL%20with%20Python" title="LinkedIn" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/linkedin.png" width="16" height="16" alt="LinkedIn"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F07%2Fextending-postgresql-with-python%2F&amp;linkname=Extending%20PostgreSQL%20with%20Python" title="Twitter" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F07%2Fextending-postgresql-with-python%2F&amp;title=Extending%20PostgreSQL%20with%20Python" id="wpa2a_2">Share</a></p><p>Related posts:<ol>
<li><a href='http://blog.madpython.com/2009/03/06/fixing-custom-sequences-in-postgresql/' rel='bookmark' title='Fixing custom sequences in PostgreSQL'>Fixing custom sequences in PostgreSQL</a></li>
<li><a href='http://blog.madpython.com/2009/03/01/ahh-those-cool-little-cli-tools/' rel='bookmark' title='Ahh those cool little CLI tools&#8230;'>Ahh those cool little CLI tools&#8230;</a></li>
<li><a href='http://blog.madpython.com/2009/03/02/apache2-shortcuts/' rel='bookmark' title='Apache2 shortcuts'>Apache2 shortcuts</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.madpython.com/2009/03/07/extending-postgresql-with-python/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Fixing custom sequences in PostgreSQL</title>
		<link>http://blog.madpython.com/2009/03/06/fixing-custom-sequences-in-postgresql/</link>
		<comments>http://blog.madpython.com/2009/03/06/fixing-custom-sequences-in-postgresql/#comments</comments>
		<pubDate>Sat, 07 Mar 2009 00:52:47 +0000</pubDate>
		<dc:creator>Xavier</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[unix]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://blog.madpython.com/?p=35</guid>
		<description><![CDATA[PostgreSQL provides a mechanism called sequences, which I believe is extracted from ANSI-SQL92, though I&#8217;m too lazy to check, which are basically stateful counters that provide some helper functions. The primary use of sequences in PostgreSQL and Oracle, is to &#8230; <a href="http://blog.madpython.com/2009/03/06/fixing-custom-sequences-in-postgresql/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://blog.madpython.com/2009/03/07/extending-postgresql-with-python/' rel='bookmark' title='Extending PostgreSQL with Python'>Extending PostgreSQL with Python</a></li>
<li><a href='http://blog.madpython.com/2009/03/02/apache2-shortcuts/' rel='bookmark' title='Apache2 shortcuts'>Apache2 shortcuts</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>PostgreSQL provides a mechanism called sequences, which I believe is extracted from ANSI-SQL92, though I&#8217;m too lazy to check, which are basically stateful counters that provide some helper functions. The primary use of sequences in PostgreSQL and Oracle, is to implement auto-incrementing counters as a table field.</p>
<p>PostgreSQL will automatically create a sequence when you use the &#8220;SERIAL&#8221; datatype for your field, and will take care of assigning the default value of your field as the result of the nextval() call on the sequence, so most of the time you don&#8217;t need to interact directly with sequences.</p>
<p>Where things can get hairy however, is when you back up your data using pg_dump or any other mechanism, and restore that data in a table that is already populated. A common scenario for example, is populating a table that already has some recent data, with some older data you&#8217;ve been storing in archival. The opposite is true if you are trying to archive data from a table to a backup database for example.</p>
<p>Whenever you manually have to provide a value for the field assigned to a sequence, you are pretty much guaranteed to break the sequence unless you take the time to nextval() your sequence until it is in sync. This is a real problem, as pg_dump does not include sequence synchronization in its output.</p>
<p>The quickest way to synchronize a sequence, based on my observations, is to run the following query, taking care to replace the name of the sequence [SEQ] and the name of the associated table [TABLE] and field [FIELD]:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">SETVAL</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>SEQ<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">COALESCE</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">FIELD</span><span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">TABLE</span><span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">FIELD</span><span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">DESC</span> <span style="color: #993333; font-weight: bold;">LIMIT</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This will fetch the highest value of [FIELD] in [TABLE], increment it by 1, and synchronize the sequence [SEQ] to the new value.</p>
<p>I&#8217;ve also written the following little Python script that will look for any <strong><em>non-system</em></strong> sequence in the specified database, and use this method to repair it. Let me know if it works out for you, or if you&#8217;d like to suggest some improvements.</p>
<p><strong>Requirements</strong>: Python 2.5+, PsycoPG2 Python module (python-psycopg2)</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;"># Standard imports</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">optparse</span> <span style="color: #ff7700;font-weight:bold;">import</span> OptionParser
&nbsp;
<span style="color: #808080; font-style: italic;"># psycopg2 import</span>
<span style="color: #ff7700;font-weight:bold;">try</span>:
    <span style="color: #ff7700;font-weight:bold;">import</span> psycopg2
<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ImportError</span>, e:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'You must install the python module named &quot;psycopg2&quot; in order to use this module.'</span>
    <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">EX_SOFTWARE</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> PgRepairman:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, options, <span style="color: #dc143c;">parser</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">options</span> = options
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            dsn = <span style="color: #483d8b;">&quot;dbname=%s host=%s user=%s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>options.<span style="color: black;">db</span>, options.<span style="color: black;">host</span>, options.<span style="color: #dc143c;">user</span><span style="color: black;">&#41;</span>
            dsn += <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&quot;</span> <span style="color: #66cc66;">!</span>= options.<span style="color: black;">passwd</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;password=%s&quot;</span> <span style="color: #66cc66;">%</span> options.<span style="color: black;">password</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #483d8b;">&quot;&quot;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">conn</span> = psycopg2.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>dsn<span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">curs</span> = <span style="color: #008000;">self</span>.<span style="color: black;">conn</span>.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span>, e:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;ERROR - %s&quot;</span> <span style="color: #66cc66;">%</span> e
            <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;"># Returns a dict for a psycopg2 row object</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> _to_dict<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, desc, res<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span><span style="color: #008000;">zip</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>x<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> desc<span style="color: black;">&#93;</span>, res<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;"># Attempt to locate all custom sequences</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> findSequences<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        seq_query = <span style="color: #483d8b;">&quot;&quot;&quot;
        SELECT pc1.relname AS seq, pc2.relname AS table, c.attname AS field 
        FROM pg_depend, pg_class pc1, pg_class pc2, pg_attribute c 
        WHERE pc1.oid = pg_depend.objid 
            AND pc2.oid = pg_depend.refobjid 
            AND c.attnum = pg_depend.refobjsubid 
            AND c.attrelid = pc2.oid 
            AND pc1.relkind = 'S' 
            AND pc1.relname NOT LIKE 'pg_toast%%'
        &quot;&quot;&quot;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">print_verbose</span><span style="color: black;">&#40;</span>seq_query<span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">curs</span>.<span style="color: black;">execute</span><span style="color: black;">&#40;</span>seq_query<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span>, e:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;[ERROR] - %s&quot;</span> <span style="color: #66cc66;">%</span> e
            <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
        desc = <span style="color: #008000;">self</span>.<span style="color: black;">curs</span>.<span style="color: black;">description</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> row <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">curs</span>.<span style="color: black;">fetchall</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">yield</span> <span style="color: #008000;">self</span>._to_dict<span style="color: black;">&#40;</span>desc, row<span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;"># Increment the key value to the value of the sequence + 1</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> fixSequences<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> seq <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">findSequences</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Fixing sequence %s in table %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>seq<span style="color: black;">&#91;</span><span style="color: #483d8b;">'seq'</span><span style="color: black;">&#93;</span>, seq<span style="color: black;">&#91;</span><span style="color: #483d8b;">'table'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
            fix_query = <span style="color: #483d8b;">&quot;SELECT setval('%s', COALESCE((SELECT %s FROM %s ORDER BY %s DESC LIMIT 1), 0)+1)&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>seq<span style="color: black;">&#91;</span><span style="color: #483d8b;">'seq'</span><span style="color: black;">&#93;</span>, seq<span style="color: black;">&#91;</span><span style="color: #483d8b;">'field'</span><span style="color: black;">&#93;</span>, seq<span style="color: black;">&#91;</span><span style="color: #483d8b;">'table'</span><span style="color: black;">&#93;</span>, seq<span style="color: black;">&#91;</span><span style="color: #483d8b;">'field'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">try</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">print_verbose</span><span style="color: black;">&#40;</span>fix_query<span style="color: black;">&#41;</span>
                <span style="color: #008000;">self</span>.<span style="color: black;">curs</span>.<span style="color: black;">execute</span><span style="color: black;">&#40;</span>fix_query<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span>, e:
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;[WARNING] - %s&quot;</span> <span style="color: #66cc66;">%</span> e
                <span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> print_verbose<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, msg<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: #008000;">True</span> == <span style="color: #008000;">self</span>.<span style="color: black;">options</span>.<span style="color: black;">verbose</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;[DEBUG] - %s&quot;</span> <span style="color: #66cc66;">%</span> msg
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__==<span style="color: #483d8b;">'__main__'</span>:
    usage       = <span style="color: #483d8b;">&quot;Usage: %prog &lt;options&gt; [-v --verbose] [-u --username | -p --password <span style="color: #000099; font-weight: bold;">\ </span>-o --host | -d --database]&quot;</span>
    version     = <span style="color: #483d8b;">&quot;%prog v1.0<span style="color: #000099; font-weight: bold;">\n</span>Distributed under the LGPL2 License&quot;</span>
    description = <span style="color: #483d8b;">&quot;Increments all sequences in a PostgreSQL database&quot;</span>
    <span style="color: #dc143c;">parser</span> = OptionParser<span style="color: black;">&#40;</span>usage=usage, version=version, description=description<span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-v&quot;</span>, <span style="color: #483d8b;">&quot;--verbose&quot;</span>, action=<span style="color: #483d8b;">&quot;store_true&quot;</span>, dest=<span style="color: #483d8b;">&quot;verbose&quot;</span>, default=<span style="color: #008000;">False</span>, <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;Enable extra output&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-o&quot;</span>, <span style="color: #483d8b;">&quot;--host&quot;</span>, action=<span style="color: #483d8b;">&quot;store&quot;</span>, dest=<span style="color: #483d8b;">&quot;host&quot;</span>, default=<span style="color: #483d8b;">&quot;127.0.0.1&quot;</span>, <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;Database hostname/IP&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-u&quot;</span>, <span style="color: #483d8b;">&quot;--username&quot;</span>, action=<span style="color: #483d8b;">&quot;store&quot;</span>, dest=<span style="color: #483d8b;">&quot;user&quot;</span>, default=<span style="color: #483d8b;">&quot;postgres&quot;</span>, <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;Database Username&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-p&quot;</span>, <span style="color: #483d8b;">&quot;--password&quot;</span>, action=<span style="color: #483d8b;">&quot;store&quot;</span>, dest=<span style="color: #483d8b;">&quot;passwd&quot;</span>, default=<span style="color: #483d8b;">&quot;&quot;</span>, <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;Database Password&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-d&quot;</span>, <span style="color: #483d8b;">&quot;--database&quot;</span>, action=<span style="color: #483d8b;">&quot;store&quot;</span>, dest=<span style="color: #483d8b;">&quot;db&quot;</span>, default=<span style="color: #483d8b;">&quot;template1&quot;</span>, <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;Database Name&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        <span style="color: black;">&#40;</span>options, args<span style="color: black;">&#41;</span> = <span style="color: #dc143c;">parser</span>.<span style="color: black;">parse_args</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        obj = PgRepairman<span style="color: black;">&#40;</span>options, <span style="color: #dc143c;">parser</span><span style="color: black;">&#41;</span>
        obj.<span style="color: black;">fixSequences</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">KeyboardInterrupt</span>, e:
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span></pre></div></div>

<p>You can also download the script <a href="http://blog.madpython.com/wp-content/uploads/2009/03/pg_sequences.py" onclick="return TrackClick('http%3A%2F%2Fblog.madpython.com%2Fwp-content%2Fuploads%2F2009%2F03%2Fpg_sequences.py','here')">here</a>.</p>
<p><a class="a2a_button_reddit" href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F06%2Ffixing-custom-sequences-in-postgresql%2F&amp;linkname=Fixing%20custom%20sequences%20in%20PostgreSQL" title="Reddit" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F06%2Ffixing-custom-sequences-in-postgresql%2F&amp;linkname=Fixing%20custom%20sequences%20in%20PostgreSQL" title="Delicious" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F06%2Ffixing-custom-sequences-in-postgresql%2F&amp;linkname=Fixing%20custom%20sequences%20in%20PostgreSQL" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F06%2Ffixing-custom-sequences-in-postgresql%2F&amp;linkname=Fixing%20custom%20sequences%20in%20PostgreSQL" title="Facebook" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F06%2Ffixing-custom-sequences-in-postgresql%2F&amp;linkname=Fixing%20custom%20sequences%20in%20PostgreSQL" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a><a class="a2a_button_linkedin" href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F06%2Ffixing-custom-sequences-in-postgresql%2F&amp;linkname=Fixing%20custom%20sequences%20in%20PostgreSQL" title="LinkedIn" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/linkedin.png" width="16" height="16" alt="LinkedIn"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F06%2Ffixing-custom-sequences-in-postgresql%2F&amp;linkname=Fixing%20custom%20sequences%20in%20PostgreSQL" title="Twitter" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F06%2Ffixing-custom-sequences-in-postgresql%2F&amp;title=Fixing%20custom%20sequences%20in%20PostgreSQL" id="wpa2a_4">Share</a></p><p>Related posts:<ol>
<li><a href='http://blog.madpython.com/2009/03/07/extending-postgresql-with-python/' rel='bookmark' title='Extending PostgreSQL with Python'>Extending PostgreSQL with Python</a></li>
<li><a href='http://blog.madpython.com/2009/03/02/apache2-shortcuts/' rel='bookmark' title='Apache2 shortcuts'>Apache2 shortcuts</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.madpython.com/2009/03/06/fixing-custom-sequences-in-postgresql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Apache2 shortcuts</title>
		<link>http://blog.madpython.com/2009/03/02/apache2-shortcuts/</link>
		<comments>http://blog.madpython.com/2009/03/02/apache2-shortcuts/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 15:59:50 +0000</pubDate>
		<dc:creator>Xavier</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[unix]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://blog.madpython.com/?p=29</guid>
		<description><![CDATA[I&#8217;ve been maintaining and managing Apache servers for over 10 years now, but for some reason, I never bothered to RTFM when it came to enabling/disabling modules and site configs in apache2.. As it turns out, you don&#8217;t have to &#8230; <a href="http://blog.madpython.com/2009/03/02/apache2-shortcuts/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://blog.madpython.com/2009/03/07/extending-postgresql-with-python/' rel='bookmark' title='Extending PostgreSQL with Python'>Extending PostgreSQL with Python</a></li>
<li><a href='http://blog.madpython.com/2009/03/06/fixing-custom-sequences-in-postgresql/' rel='bookmark' title='Fixing custom sequences in PostgreSQL'>Fixing custom sequences in PostgreSQL</a></li>
<li><a href='http://blog.madpython.com/2009/03/01/ahh-those-cool-little-cli-tools/' rel='bookmark' title='Ahh those cool little CLI tools&#8230;'>Ahh those cool little CLI tools&#8230;</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been maintaining and managing Apache servers for over 10 years now, but for some reason, I never bothered to RTFM when it came to enabling/disabling modules and site configs in apache2.. As it turns out, you don&#8217;t have to manually create symlinks from mods_available to mods_enabled and sites_available to sites_enabled, as Apache2 includes a handful of shortcut scripts to do this work for you&#8230; Doh!</p>
<p>To enable a module in your apache2 config, instead of doing the old</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-sf</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>mods_available<span style="color: #000000; font-weight: bold;">/</span>mod_rewrite.conf <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>mods_enabled<span style="color: #000000; font-weight: bold;">/</span>mod_rewrite.conf
<span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-sf</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>mods_available<span style="color: #000000; font-weight: bold;">/</span>mod_rewrite.load <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>mods_enabled<span style="color: #000000; font-weight: bold;">/</span>mod_rewrite.load</pre></div></div>

<p>Next time, just do:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">a2enmod rewrite <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 restart</pre></div></div>

<p>To disable this module, try</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">a2dismod rewrite <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 restart</pre></div></div>

<p>Similarly, to enable a site config, try</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">a2ensite myVhost.com <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 restart</pre></div></div>

<p> and to disable it, obviously, try</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">a2dissite myVhost.com <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 restart</pre></div></div>

<p>Finally, if you&#8217;d like to lint through your Apache2 config files before issuing a restart which might be responsible for some downtime if it doesn&#8217;t work, try</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">apache2ctl configtest</pre></div></div>

<p><a class="a2a_button_reddit" href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F02%2Fapache2-shortcuts%2F&amp;linkname=Apache2%20shortcuts" title="Reddit" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F02%2Fapache2-shortcuts%2F&amp;linkname=Apache2%20shortcuts" title="Delicious" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F02%2Fapache2-shortcuts%2F&amp;linkname=Apache2%20shortcuts" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F02%2Fapache2-shortcuts%2F&amp;linkname=Apache2%20shortcuts" title="Facebook" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F02%2Fapache2-shortcuts%2F&amp;linkname=Apache2%20shortcuts" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a><a class="a2a_button_linkedin" href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F02%2Fapache2-shortcuts%2F&amp;linkname=Apache2%20shortcuts" title="LinkedIn" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/linkedin.png" width="16" height="16" alt="LinkedIn"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F02%2Fapache2-shortcuts%2F&amp;linkname=Apache2%20shortcuts" title="Twitter" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F02%2Fapache2-shortcuts%2F&amp;title=Apache2%20shortcuts" id="wpa2a_6">Share</a></p><p>Related posts:<ol>
<li><a href='http://blog.madpython.com/2009/03/07/extending-postgresql-with-python/' rel='bookmark' title='Extending PostgreSQL with Python'>Extending PostgreSQL with Python</a></li>
<li><a href='http://blog.madpython.com/2009/03/06/fixing-custom-sequences-in-postgresql/' rel='bookmark' title='Fixing custom sequences in PostgreSQL'>Fixing custom sequences in PostgreSQL</a></li>
<li><a href='http://blog.madpython.com/2009/03/01/ahh-those-cool-little-cli-tools/' rel='bookmark' title='Ahh those cool little CLI tools&#8230;'>Ahh those cool little CLI tools&#8230;</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.madpython.com/2009/03/02/apache2-shortcuts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ahh those cool little CLI tools&#8230;</title>
		<link>http://blog.madpython.com/2009/03/01/ahh-those-cool-little-cli-tools/</link>
		<comments>http://blog.madpython.com/2009/03/01/ahh-those-cool-little-cli-tools/#comments</comments>
		<pubDate>Sun, 01 Mar 2009 19:37:19 +0000</pubDate>
		<dc:creator>Xavier</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[unix]]></category>
		<category><![CDATA[admin]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://blog.madpython.com/?p=4</guid>
		<description><![CDATA[The list of Unix/Linux utilities available grows every day. Here&#8217;s a little list of cherry-picked utilities i&#8217;ve found myself using more and more lately&#8230; Inotail: Inotail uses the Linux kernel&#8217;s inotify API, which was implemented with v2.6.13 to monitor changes &#8230; <a href="http://blog.madpython.com/2009/03/01/ahh-those-cool-little-cli-tools/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://blog.madpython.com/2009/03/07/extending-postgresql-with-python/' rel='bookmark' title='Extending PostgreSQL with Python'>Extending PostgreSQL with Python</a></li>
<li><a href='http://blog.madpython.com/2009/03/02/apache2-shortcuts/' rel='bookmark' title='Apache2 shortcuts'>Apache2 shortcuts</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The list of Unix/Linux utilities available grows every day. Here&#8217;s a little list of cherry-picked utilities i&#8217;ve found myself using more and more lately&#8230;</p>
<h4>Inotail:</h4>
<p>Inotail uses the Linux kernel&#8217;s <em>inotify</em> API, which was implemented with v2.6.13 to monitor changes to files on the filesystem. This design is more efficient than our beloved <em>tail</em>, which relies on polling the monitored file for changes every second.  Example: To monitor in real-time syslog entries, try:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">inotail <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>messages</pre></div></div>

<p>The documentation for inotail, if you need it, can be found at <a href="http://distanz.ch/inotail/" onclick="return TrackClick('http%3A%2F%2Fdistanz.ch%2Finotail%2F','http%3A%2F%2Fdistanz.ch%2Finotail%2F')" target="_blank">http://distanz.ch/inotail/</a></p>
<h4>Incron:</h4>
<p>Incron is an event-scheduler similar to <em>cron</em>, except that it is based on file-system events as opposed to our beloved time-based <em>cron</em> daemons. It is also based on the <em>inotify</em> subsystem, which means it is only available on Linux as far as I know.  Let&#8217;s set up a quick example to demonstrate the stuff you can do with incron. We&#8217;re going to install incron, and configure it to automatically create a thumbnail of any picture dropped in a specified directory using ImageMagick&#8217;s <em>convert</em> utility, on a stock Ubuntu Linux system:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Package installation</span>
<span style="color: #c20cb9; font-weight: bold;">aptitude</span> <span style="color: #c20cb9; font-weight: bold;">install</span> incron imagemagick
<span style="color: #666666; font-style: italic;"># Add your user account to the list of allowed incron users (replace xavier by your account)</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">sh</span> <span style="color: #660033;">-c</span> <span style="color: #ff0000;">&quot;echo xavier &gt;&gt; /etc/incron.allow&quot;</span>
<span style="color: #666666; font-style: italic;"># Create our directory structure</span>
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>xavier<span style="color: #000000; font-weight: bold;">/</span>images<span style="color: #000000; font-weight: bold;">/</span>original
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>xavier<span style="color: #000000; font-weight: bold;">/</span>images<span style="color: #000000; font-weight: bold;">/</span>thumb
<span style="color: #666666; font-style: italic;"># Edit the actual incron file</span>
incrontab <span style="color: #660033;">--edit</span></pre></div></div>

<p>The editor will now fire up. Enter the following lines in the editor, and exit:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Convert /home/xavier/images/original/test.png to /home/xavier/images/thumb/test.png</span>
<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>xavier<span style="color: #000000; font-weight: bold;">/</span>images<span style="color: #000000; font-weight: bold;">/</span>original<span style="color: #000000; font-weight: bold;">/</span>   IN_CLOSE_WRITE    convert <span style="color: #660033;">-thumbnail</span> 320x320 $<span style="color: #000000; font-weight: bold;">@/</span><span style="color: #007800;">$#</span> $<span style="color: #000000; font-weight: bold;">@/</span>..<span style="color: #000000; font-weight: bold;">/</span>thumb<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$#</span>
<span style="color: #666666; font-style: italic;"># When an original is deleted, automatically clean up the associated thumbnail</span>
<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>xavier<span style="color: #000000; font-weight: bold;">/</span>images<span style="color: #000000; font-weight: bold;">/</span>original<span style="color: #000000; font-weight: bold;">/</span>   IN_DELETE    <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> $<span style="color: #000000; font-weight: bold;">@/</span>..<span style="color: #000000; font-weight: bold;">/</span>thumb<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$#</span></pre></div></div>

<p>We&#8217;re all done. Any new image dropped in /home/xavier/images/original/ will automatically be converted into a thumbnail of the same name in /home/xavier/images/thumb/.  There are many things you can do with incron, so i suggest you check out the following links:</p>
<ul>
<li>Incron Documentation: <a href="http://inotify.aiken.cz/?section=incron&amp;page=doc&amp;lang=en" onclick="return TrackClick('http%3A%2F%2Finotify.aiken.cz%2F%3Fsection%3Dincron%26amp%3Bpage%3Ddoc%26amp%3Blang%3Den','http%3A%2F%2Finotify.aiken.cz%2F%3Fsection%3Dincron%26amp%3Bpage%3Ddoc%26amp%3Blang%3Den')" target="_blank">http://inotify.aiken.cz/?section=incron&amp;page=doc&amp;lang=en</a></li>
<li>Incron FAQ: <a href="http://inotify.aiken.cz/?section=incron&amp;page=faq&amp;lang=en" onclick="return TrackClick('http%3A%2F%2Finotify.aiken.cz%2F%3Fsection%3Dincron%26amp%3Bpage%3Dfaq%26amp%3Blang%3Den','http%3A%2F%2Finotify.aiken.cz%2F%3Fsection%3Dincron%26amp%3Bpage%3Dfaq%26amp%3Blang%3Den')" target="_blank">http://inotify.aiken.cz/?section=incron&amp;page=faq&amp;lang=en</a></li>
<li>inotify.h: <a href="http://www.kernel.org/pub/linux/kernel/people/rml/inotify/headers/inotify.h" onclick="return TrackClick('http%3A%2F%2Fwww.kernel.org%2Fpub%2Flinux%2Fkernel%2Fpeople%2Frml%2Finotify%2Fheaders%2Finotify.h','http%3A%2F%2Fwww.kernel.org%2Fpub%2Flinux%2Fkernel%2Fpeople%2Frml%2Finotify%2Fheaders%2Finotify.h')" target="_blank">http://www.kernel.org/pub/linux/kernel/people/rml/inotify/headers/inotify.h</a></li>
</ul>
<h4>ccze</h4>
<p>ccze is simply a logfile syntax highlighter for various file-formats commonly found on unix systems, such as <em>syslog, apache logs, dmesg</em>, etc&#8230;  You can have it syntax-highlight a file in your terminal by using the following syntax:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">ccze <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>messages</pre></div></div>

<p>Or you can pipe anything onto ccze to have it stream syntax-coloured output on your terminal. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">inotail <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>messages <span style="color: #000000; font-weight: bold;">|</span> ccze <span style="color: #660033;">-A</span></pre></div></div>

<p>Additionally, ccze can also output syntax-coloured text in HTML. For example, the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">dmesg</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-i</span> cpu <span style="color: #000000; font-weight: bold;">|</span> ccze <span style="color: #660033;">-m</span> html</pre></div></div>

<p>Woud output the following document: <a href="http://blog.madpython.com/wp-content/uploads/2009/03/cpu.html" onclick="return TrackClick('http%3A%2F%2Fblog.madpython.com%2Fwp-content%2Fuploads%2F2009%2F03%2Fcpu.html','ccze+Output')">ccze Output</a></p>
<p><a class="a2a_button_reddit" href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F01%2Fahh-those-cool-little-cli-tools%2F&amp;linkname=Ahh%20those%20cool%20little%20CLI%20tools%26%238230%3B" title="Reddit" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F01%2Fahh-those-cool-little-cli-tools%2F&amp;linkname=Ahh%20those%20cool%20little%20CLI%20tools%26%238230%3B" title="Delicious" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F01%2Fahh-those-cool-little-cli-tools%2F&amp;linkname=Ahh%20those%20cool%20little%20CLI%20tools%26%238230%3B" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F01%2Fahh-those-cool-little-cli-tools%2F&amp;linkname=Ahh%20those%20cool%20little%20CLI%20tools%26%238230%3B" title="Facebook" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F01%2Fahh-those-cool-little-cli-tools%2F&amp;linkname=Ahh%20those%20cool%20little%20CLI%20tools%26%238230%3B" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a><a class="a2a_button_linkedin" href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F01%2Fahh-those-cool-little-cli-tools%2F&amp;linkname=Ahh%20those%20cool%20little%20CLI%20tools%26%238230%3B" title="LinkedIn" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/linkedin.png" width="16" height="16" alt="LinkedIn"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F01%2Fahh-those-cool-little-cli-tools%2F&amp;linkname=Ahh%20those%20cool%20little%20CLI%20tools%26%238230%3B" title="Twitter" rel="nofollow" target="_blank"><img src="http://blog.madpython.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.madpython.com%2F2009%2F03%2F01%2Fahh-those-cool-little-cli-tools%2F&amp;title=Ahh%20those%20cool%20little%20CLI%20tools%26%238230%3B" id="wpa2a_8">Share</a></p><p>Related posts:<ol>
<li><a href='http://blog.madpython.com/2009/03/07/extending-postgresql-with-python/' rel='bookmark' title='Extending PostgreSQL with Python'>Extending PostgreSQL with Python</a></li>
<li><a href='http://blog.madpython.com/2009/03/02/apache2-shortcuts/' rel='bookmark' title='Apache2 shortcuts'>Apache2 shortcuts</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.madpython.com/2009/03/01/ahh-those-cool-little-cli-tools/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

