<?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>Mkyong Dot Com &#187; postgresql</title>
	<atom:link href="http://www.mkyong.com/tag/postgresql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mkyong.com</link>
	<description>Thoughts on Java, Maven, Hibernate, Spring , Ajax web development</description>
	<lastBuildDate>Sun, 14 Mar 2010 01:13:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Regular Expression in PostgreSQL</title>
		<link>http://www.mkyong.com/database/regular-expression-in-postgresql/</link>
		<comments>http://www.mkyong.com/database/regular-expression-in-postgresql/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 09:55:25 +0000</pubDate>
		<dc:creator>mkyong</dc:creator>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[isdigit]]></category>
		<category><![CDATA[isipaddress]]></category>
		<category><![CDATA[isstring]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[Regular Expression]]></category>

		<guid isPermaLink="false">http://www.mkyong.com/database/regular-expression-in-postgresql/</guid>
		<description><![CDATA[Regular Expression is a very powerful tools for programming language like java, .NET, PHP , Perl&#8230;or even PostgreSQL.
Here i write some basic examples to show how to use regular expression in PostgreSQL.
1) isdigit function &#8211; This function is missing in PostgreSQL, as this is a built-in function in others database.
This is a very useful function [...]]]></description>
			<content:encoded><![CDATA[<p>Regular Expression is a very powerful tools for programming language like java, .NET, PHP , Perl&#8230;or even PostgreSQL.<br />
Here i write some basic examples to show how to use regular expression in PostgreSQL.</p>
<p>1) <b>isdigit function</b> &#8211; This function is missing in PostgreSQL, as this is a built-in function in others database.<br />
This is a very useful function to validate only number allow. However we can create isdigit function ourself in PostgreSQL.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td 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;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> isdigit<span style="color: #66cc66;">&#40;</span>text<span style="color: #66cc66;">&#41;</span> returns <span style="color: #993333; font-weight: bold;">BOOLEAN</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'
select $1 ~ '</span><span style="color: #ff0000;">'^(-)?[0-9]+$'</span><span style="color: #ff0000;">' as result
'</span> <span style="color: #993333; font-weight: bold;">LANGUAGE</span> sql;
&nbsp;
<span style="color: #808080; font-style: italic;">--test function</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> isdigit<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'1'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">--return true</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> isdigit<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'A'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">--return false</span></pre></td></tr></table></div>

<p>2) <b>isString function</b> &#8211; This function is missing in PostgreSQL as well, as this is a built-in function in others database.<br />
This is a very useful function to validate only string allow. However we can create isString function ourself in PostgreSQL.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td 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;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> isString<span style="color: #66cc66;">&#40;</span>text<span style="color: #66cc66;">&#41;</span> returns <span style="color: #993333; font-weight: bold;">BOOLEAN</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'
select $1 ~ '</span><span style="color: #ff0000;">'^(-)?[a-zA-Z]+$'</span><span style="color: #ff0000;">' as result
'</span> <span style="color: #993333; font-weight: bold;">LANGUAGE</span> sql;
&nbsp;
<span style="color: #808080; font-style: italic;">--test function</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> isString<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'1'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">--return false</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> isString<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'A'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">--return true</span></pre></td></tr></table></div>

<p>3) <b>isIPAddress function</b> &#8211; I do not think this is a built-in function in other database. This functions is use to validate IP Address allow. </p>
<p>Example A</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">--check ipaddress</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> isIPAddress<span style="color: #66cc66;">&#40;</span>text<span style="color: #66cc66;">&#41;</span> returns <span style="color: #993333; font-weight: bold;">BOOLEAN</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'
select $1 ~ '</span><span style="color: #ff0000;">'^[0-9]+<span style="color: #000099; font-weight: bold;">\.</span>[0-9]+<span style="color: #000099; font-weight: bold;">\.</span>[0-9]+<span style="color: #000099; font-weight: bold;">\.</span>[0-9]+$'</span><span style="color: #ff0000;">' as result
'</span> <span style="color: #993333; font-weight: bold;">LANGUAGE</span> sql;
&nbsp;
<span style="color: #808080; font-style: italic;">--test function</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> isIPAddress<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'202.111.0.1'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">--return true</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> isIPAddress<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'202.ZZZ.0.A'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">--return false</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> isIPAddress<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'202.11199999999.1.100'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">--return true</span></pre></td></tr></table></div>

<p>Example B</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td 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;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> isIPAddressStrict<span style="color: #66cc66;">&#40;</span>text<span style="color: #66cc66;">&#41;</span> returns <span style="color: #993333; font-weight: bold;">BOOLEAN</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'
select $1 ~ '</span><span style="color: #ff0000;">'^[0-9]?[0-9]?[0-9]?<span style="color: #000099; font-weight: bold;">\.</span>[0-9]?[0-9]?[0-9]?<span style="color: #000099; font-weight: bold;">\.</span>[0-9]?[0-9]?[0-9]?<span style="color: #000099; font-weight: bold;">\.</span>[0-9]?[0-9]?[0-9]?$'</span><span style="color: #ff0000;">' as result
'</span> <span style="color: #993333; font-weight: bold;">LANGUAGE</span> sql;
&nbsp;
<span style="color: #808080; font-style: italic;">--test function</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> isIPAddressStrict<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'202.111.0.1'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">--return true</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> isIPAddressStrict<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'202.ZZZ.0.A'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">--return false</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> isIPAddressStrict<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'202.11199999999.1.100'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">--return false</span></pre></td></tr></table></div>

<p>Wow, impressive right? Actually Regular Expression is more powerful than above simple function. It is worth to invest time to study on it. Please go here if you want to know more about it<br />
<a href=http://en.wikipedia.org/wiki/Regular_expression target='_blank'>http://en.wikipedia.org/wiki/Regular_expression</a></p>
<p>Please share your example to me also, if you do not mind. Thanks</p>
<ul class="related_post"><li><a href="http://www.mkyong.com/database/postgresql-error-operator-does-not-exist-smallint-character-varying-solution/" title="PostgreSQL &#8211; ERROR: operator does not exist: smallint = character varying (Solution)">PostgreSQL &#8211; ERROR: operator does not exist: smallint = character varying (Solution)</a></li><li><a href="http://www.mkyong.com/database/how-to-export-table-data-to-file-csv-postgresql/" title="How to export table data to file / csv &#8211; PostgreSQL">How to export table data to file / csv &#8211; PostgreSQL</a></li><li><a href="http://www.mkyong.com/database/how-to-kill-terminate-postgresql-hang-query/" title="How to kill / terminate PostgreSQL hang query">How to kill / terminate PostgreSQL hang query</a></li><li><a href="http://www.mkyong.com/database/to_date-function-between-postgresql-82-and-83/" title="TO_DATE function between PostgreSQL 8.2 and 8.3">TO_DATE function between PostgreSQL 8.2 and 8.3</a></li><li><a href="http://www.mkyong.com/database/backup-restore-database-in-postgresql-pg_dumppg_restore/" title="Backup &#038; Restore Database in PostgreSQL (pg_dump,pg_restore)">Backup &#038; Restore Database in PostgreSQL (pg_dump,pg_restore)</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mkyong.com/database/regular-expression-in-postgresql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to connect to PostgreSQL with JDBC driver &#8211; Java</title>
		<link>http://www.mkyong.com/java/how-do-connect-to-postgresql-with-jdbc-driver-java/</link>
		<comments>http://www.mkyong.com/java/how-do-connect-to-postgresql-with-jdbc-driver-java/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 09:25:31 +0000</pubDate>
		<dc:creator>mkyong</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[jdbc]]></category>
		<category><![CDATA[postgresql]]></category>

		<guid isPermaLink="false">http://www.mkyong.com/?p=750</guid>
		<description><![CDATA[Here is a simple example to demonstrate how do connect to PostgreSQL database with JDBC driver in Java.
Go get a PostgreSQL JDBC driver first , else we can not do anything PostgreSQL JDBC Driver Download Here

Java JDBC connection always behave like following

Class.forName&#40;&#34;org.postgresql.Driver&#34;&#41;;
Connection connection = null;
connection = DriverManager.getConnection&#40;
&#34;jdbc:postgresql://hostname:port/dbname&#34;,&#34;username&#34;, &#34;password&#34;&#41;;
connection.close&#40;&#41;;

Here is the full example

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
&#160;
public [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a simple example to demonstrate <b>how do connect to PostgreSQL database with JDBC driver in Java</b>.</p>
<p>Go get a PostgreSQL JDBC driver first , else we can not do anything <a href="http://jdbc.postgresql.org/download.html" target="_blank">PostgreSQL JDBC Driver Download Here<br />
</a></p>
<p>Java JDBC connection always behave like following</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006633;">forName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;org.postgresql.Driver&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Connection</span> connection <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
connection <span style="color: #339933;">=</span> <span style="color: #003399;">DriverManager</span>.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">&quot;jdbc:postgresql://hostname:port/dbname&quot;</span>,<span style="color: #0000ff;">&quot;username&quot;</span>, <span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
connection.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here is the full example</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.DriverManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Connection</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.SQLException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JDBCExample <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> argv<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;-------- PostgreSQL JDBC Connection Testing ------------&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	  <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006633;">forName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;org.postgresql.Driver&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	  <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">ClassNotFoundException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Where is your PostgreSQL JDBC Driver? Include in your library path!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
	  <span style="color: #009900;">&#125;</span>
&nbsp;
	  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PostgreSQL JDBC Driver Registered!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	  <span style="color: #003399;">Connection</span> connection <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
	  <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		 connection <span style="color: #339933;">=</span> <span style="color: #003399;">DriverManager</span>.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jdbc:postgresql://127.0.0.1:5432/testdb&quot;</span>,<span style="color: #0000ff;">&quot;mkyong&quot;</span>, <span style="color: #0000ff;">&quot;123456&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	  <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">SQLException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Connection Failed! Check output console&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
	  <span style="color: #009900;">&#125;</span>
&nbsp;
	  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>connection <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
		  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;You made it, take control your database now!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #000000; font-weight: bold;">else</span>
	          <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Failed to make connection!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>How to run it?</h3>
<p>Assume JDBCExample is store in c:\test folder, together with mysql JDBC driver</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">C:\<span style="color: #7a0874; font-weight: bold;">test</span><span style="color: #000000; font-weight: bold;">&gt;</span>java <span style="color: #660033;">-cp</span> c:\<span style="color: #7a0874; font-weight: bold;">test</span>\postgresql-<span style="color: #000000;">8.3</span>-603.jdbc4.jar;c:\<span style="color: #7a0874; font-weight: bold;">test</span> JDBCExample
<span style="color: #660033;">--------</span> MySQL JDBC Connection Testing <span style="color: #660033;">------------</span>
PostgreSQL JDBC Driver Registered<span style="color: #000000; font-weight: bold;">!</span>
You made it, take control your database now<span style="color: #000000; font-weight: bold;">!</span>
&nbsp;
C:\<span style="color: #7a0874; font-weight: bold;">test</span><span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<ul class="related_post"><li><a href="http://www.mkyong.com/java/bind-variables-performance-test-in-java/" title="Bind variables Performance Test in Java">Bind variables Performance Test in Java</a></li><li><a href="http://www.mkyong.com/spring/spring-jdbctemplate-jdbcdaosupport-examples/" title="Spring + JdbcTemplate + JdbcDaoSupport examples">Spring + JdbcTemplate + JdbcDaoSupport examples</a></li><li><a href="http://www.mkyong.com/spring/maven-spring-jdbc-example/" title="Maven + Spring + JDBC Example">Maven + Spring + JDBC Example</a></li><li><a href="http://www.mkyong.com/java/how-to-get-current-timestamps-in-java/" title="How to get current timestamps in Java">How to get current timestamps in Java</a></li><li><a href="http://www.mkyong.com/java/java-sha-hashing-example/" title="Java SHA Hashing Example">Java SHA Hashing Example</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mkyong.com/java/how-do-connect-to-postgresql-with-jdbc-driver-java/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>PostgreSQL Point-in-time Recovery (Incremental Backup)</title>
		<link>http://www.mkyong.com/database/postgresql-point-in-time-recovery-incremental-backup/</link>
		<comments>http://www.mkyong.com/database/postgresql-point-in-time-recovery-incremental-backup/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 09:26:45 +0000</pubDate>
		<dc:creator>mkyong</dc:creator>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[Incremental Backup]]></category>
		<category><![CDATA[Point-in-time Recovery]]></category>
		<category><![CDATA[postgresql]]></category>

		<guid isPermaLink="false">http://www.mkyong.com/?p=400</guid>
		<description><![CDATA[PostgreSQL “Point-in-time Recovery” (PITR) also called as incremental database backup , online backup or may be archive backup. The PostgreSQL server records all users’ data modification transaction like insert, update or delete and write it into a file call write-ahead (WAL) log file. This mechanism use the history records stored in WAL file to do [...]]]></description>
			<content:encoded><![CDATA[<p>PostgreSQL “Point-in-time Recovery” (PITR) also called as incremental database backup , online backup or may be archive backup. The PostgreSQL server records all users’ data modification transaction like insert, update or delete and write it into a file call write-ahead (WAL) log file. This mechanism use the history records stored in WAL file to do roll-forward changes made since last database full backup.</p>
<h2>Advantages</h2>
<p><b>1) Zero down time</b> &#8211; The incremental database backup is important to critical system that can not afford even a minute down time. With Point-in-time Recovery, database backup down time can totally eliminated because this mechanism can make database backup and system access happened at the same time.</p>
<p><b>2) Save storage size</b> &#8211; with incremental database backup, we backup the latest archive log file since last backup instead of full database backup everyday.</p>
<p>If above advantages are concern to you, then you should always implement incremental database backup. Here i demonstrate how to do the Point-in-time Recovery (Incremental Backup) in PostgreSQL server.</p>
<p><b>Summary of PostgreSQL Backup Steps</b><br />
1) Modify postgresql.conf to support archive log<br />
2) Make a base backup (full database backup)<br />
3) Backup base backup to remote storage.<br />
4) Backup WAL (archive log files) to remote storage (continuous process)</p>
<p><b>Summary of PostgreSQL Point-in-time Recovery Steps</b><br />
1) Extract files from base backup.<br />
2) Copy files from pg_xlog folder<br />
3) Create recovery.conf file<br />
4) Start Recover</p>
<p>Here demostration start ~ long journey, patient&#8230;</p>
<p><u><br />
<h2>Database initialization</h2>
<p></u><br />
<b>1) Create a testing database, all database files under /usr/local/pgsql/pgDataPITR/</b></p>
<p>Initilize database</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>mkyong<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ initdb <span style="color: #660033;">-D</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>pgDataPITR<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p>Start the database</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>mkyong<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ pg_ctl start <span style="color: #660033;">-D</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>pgDataPITR<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p><b>2) Make change in Postgresql configuration file (postgresql.conf)</b>,  we need to make some changes in postgresql.conf file to tell PostgreSQL how to copy or archive WAL files that generated from PostgreSQL server.</p>
<p>Modify postgresql.conf</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>mkyong<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">vi</span> postgresql.conf</pre></div></div>

<p>Make following changes in postgresql.conf</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">archive_command = on
archive_command = <span style="color: #ff0000;">'cp %p /usr/local/pgsql/pgDataPITR/wals/%f'</span></pre></div></div>

<p>Create a wals folder</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>mkyong<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>pgDataPITR<span style="color: #000000; font-weight: bold;">/</span>wals</pre></div></div>

<p>Restart the database</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>mkyong<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ pg_ctl stop <span style="color: #660033;">-D</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>pgDataPITR<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>mkyong<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ pg_ctl start <span style="color: #660033;">-D</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>pgDataPITR<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p><b><font color=red>Attention!!! </font> Understand how PostgreSQL handle log files, pg_xlog and archive log</b><br />
pg_xlog is a PostgreSQL log file folder that use to store all data history records. It located at /usr/local/pgsql/pgDataPITR/pg_xlog.  For example, when user inserted , update or delete a record, all transaction hisroty will automatically create or append to a file log file under pg_xlog folder. Log file format is look like following format 000000010000000000000001 -> 000000010000000000000006</p>
<p>For example,</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>postgres<span style="color: #000000; font-weight: bold;">@</span>localhost pg_xlog<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-lsh</span>
total 113M
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">25</span> 000000010000000000000006</pre></div></div>

<p>Every log file can handle around 16M data, when it excess this limit, it will automatically create a new log file, filename is follow 0-9 and A-Z</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">000000010000000000000001
..
..
000000010000000000000009
..
..
00000001000000000000000A
..
..
00000001000000000000000Z</pre></div></div>

<p>This is the log files that we going to use as the roll-forward PostgreSQL Point-in-time Recovery <img src='http://www.mkyong.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><b><font color=red>Attention!!! </font>Do you Still remember we configure the WAL filepath in postgresql.conf file?</b></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">archive_command = on
archive_command = <span style="color: #ff0000;">'cp %p /usr/local/pgsql/pgDataPITR/wals/%f'</span></pre></div></div>

<p>This means when pg_xlog folder grow to certain limitation, like 6 log files each contain 16M, when PostgreSQL try to insert a new history record and detected that pg_xlog is full, it will automatically archive the oldest history log file and move it to <b>/usr/local/pgsql/pgDataPITR/wals</b> folder. </p>
<p><b><font color=red>Attention!!! </font></b> We have to backup these archive files continuously (that why it call incremental backup <img src='http://www.mkyong.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ). We do not need to do full database backup anymore, but we do backup those archive log files constantly.</p>
<p>Important log files folder</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>pgDataPITR<span style="color: #000000; font-weight: bold;">/</span>pg_xlog
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>pgDataPITR<span style="color: #000000; font-weight: bold;">/</span>wals</pre></div></div>

<p><u><br />
<h2>Data Simulation &#038; Backup Process </h2>
<p></u><br />
   Create dummy tables and records &#8211; we will dynamic create 455,252 records in a new table, 400k records will force PostgreSQL to create enough log files in pg_xlog folder and fire the archive process to archive the log files from <b>/usr/local/pgsql/pgDataPITR/pg_xlog</b> to <b>/usr/local/pgsql/pgDataPITR/wals</b>, every logs file contain around 16M size file.</p>
<p><b>1) Table testPITR1 created at 2008-11-25 17:17</b></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># psql</span>
<span style="color: #666666; font-style: italic;"># select (*) from pg_class; –- contain 229 records</span>
<span style="color: #666666; font-style: italic;"># select (*) from pg_description; –- contains 1988 records</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># create table testPITR1 as select * from pg_class, pg_description; --totally 229 x 1988 = 455,252 records</span>
<span style="color: #666666; font-style: italic;"># select * from current_timestamp; –-2008-11-25 17:17</span></pre></div></div>

<p>Log files look like following</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>postgres<span style="color: #000000; font-weight: bold;">@</span>localhost pgDataPITR<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #7a0874; font-weight: bold;">cd</span> pg_xlog<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>postgres<span style="color: #000000; font-weight: bold;">@</span>localhost pg_xlog<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-lsh</span>
total 113M
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">25</span> 000000010000000000000006
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">16</span> 000000010000000000000007
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000008
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000009
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 00000001000000000000000A
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 00000001000000000000000B
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 00000001000000000000000C
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>postgres<span style="color: #000000; font-weight: bold;">@</span>localhost pgDataPITR<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #7a0874; font-weight: bold;">cd</span> wals
<span style="color: #7a0874; font-weight: bold;">&#91;</span>postgres<span style="color: #000000; font-weight: bold;">@</span>localhost wals<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-lsh</span>
total 97M
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000000
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000001
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000002
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000003
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000004
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000005</pre></div></div>

<p><b>2) Create a full databse backup &#8211; base backup</b></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># psql</span>
<span style="color: #666666; font-style: italic;"># select pg_start_backup(’Full Backup - Testing’);</span>
&nbsp;
pg_start_backup<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
——————–
<span style="color: #000000;">0</span><span style="color: #000000; font-weight: bold;">/</span>6BA9328
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span> row<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>pg_start_backup is use to create a label, and log it into log file. (actually this is optional, good habit)</p>
<p><b>Use a tar command to compress all pgDataPITR folder to make a database base backup.</b></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-cvzf</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>pgDataPITR<span style="color: #000000; font-weight: bold;">/</span>pgdatabk20081125.tar <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>pgDataPITR<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p><b><font color=blue>Remember !!!</font> pgdatabk20081125.tar</b> this is the full database backup (base backup) including Postgresql configuration , system and all others files and folder.</p>
<p>pg_stop_backup() create a label in log file as well. (actually this is optional, good habit)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">select</span> pg_stop_backup<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
————————
<span style="color: #000000;">0</span><span style="color: #000000; font-weight: bold;">/</span>6BA9384
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span> row<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p><b>3) Table testPITR2 created at 2008-11-25 18:08:06</b> &#8211;prepare for Point-in-time Recovery</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># psql</span>
<span style="color: #666666; font-style: italic;"># create table testPITR2 as select * from pg_class, pg_description; </span>
<span style="color: #666666; font-style: italic;"># select * from current_timestamp; --2008-11-25 18:08:06</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>postgres<span style="color: #000000; font-weight: bold;">@</span>localhost pgDataPITR<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #7a0874; font-weight: bold;">cd</span> pg_xlog<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>postgres<span style="color: #000000; font-weight: bold;">@</span>localhost pg_xlog<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-lsh</span>
total 113M
8.0K -rw——- <span style="color: #000000;">1</span> postgres postgres <span style="color: #000000;">254</span> <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:02 000000010000000000000006.00BA9328.backup
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:07 00000001000000000000000A
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:07 00000001000000000000000B
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:07 00000001000000000000000C
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:08 00000001000000000000000D
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:06 00000001000000000000000E
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:07 00000001000000000000000F
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:07 000000010000000000000010
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>postgres<span style="color: #000000; font-weight: bold;">@</span>localhost wals<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-lsh</span>
total 209M
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000000
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000001
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000002
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000003
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000004
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000005
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:02 000000010000000000000006
8.0K -rw——- <span style="color: #000000;">1</span> postgres postgres <span style="color: #000000;">254</span> <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:02 000000010000000000000006.00BA9328.backup
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:06 000000010000000000000007
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:07 000000010000000000000008
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:07 000000010000000000000009
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:07 00000001000000000000000A
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:07 00000001000000000000000B
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:07 00000001000000000000000C</pre></div></div>

<p>P.S The pg_start_backup() and pg_stop_backup() backup labels will created in <b>000000010000000000000006.00BA9328.backup</b> file. This is a good habit to make a label here.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>postgres<span style="color: #000000; font-weight: bold;">@</span>localhost wals<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">cat</span> 000000010000000000000006.00BA9328.backup
START WAL LOCATION: <span style="color: #000000;">0</span><span style="color: #000000; font-weight: bold;">/</span>6BA9328 <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">file</span> 000000010000000000000006<span style="color: #7a0874; font-weight: bold;">&#41;</span>
STOP WAL LOCATION: <span style="color: #000000;">0</span><span style="color: #000000; font-weight: bold;">/</span>6BA9384 <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">file</span> 000000010000000000000006<span style="color: #7a0874; font-weight: bold;">&#41;</span>
CHECKPOINT LOCATION: <span style="color: #000000;">0</span><span style="color: #000000; font-weight: bold;">/</span>6BA9328
START TIME: <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">45</span>:<span style="color: #000000;">24</span> MYT
LABEL: Full Backup - Testing
STOP TIME: <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:02:<span style="color: #000000;">18</span> MYT</pre></div></div>

<p><b>3) Table testPITR3 created at 2008-11-25 18:15:23 </b> &#8211;prepare for Point-in-time Recovery</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># psql</span>
<span style="color: #666666; font-style: italic;"># create table testPITR3 as select * from pg_class, pg_description; </span>
<span style="color: #666666; font-style: italic;"># select * from current_timestamp; --–2008-11-25 18:15:23</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>postgres<span style="color: #000000; font-weight: bold;">@</span>localhost pg_xlog<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-lsh</span>
total 129M
8.0K -rw——- <span style="color: #000000;">1</span> postgres postgres <span style="color: #000000;">254</span> <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:02 000000010000000000000006.00BA9328.backup
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">14</span> 000000010000000000000010
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">14</span> 000000010000000000000011
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">14</span> 000000010000000000000012
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">15</span> 000000010000000000000013
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">15</span> 000000010000000000000014
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">14</span> 000000010000000000000015
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">14</span> 000000010000000000000016
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">14</span> 000000010000000000000017
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>postgres<span style="color: #000000; font-weight: bold;">@</span>localhost wals<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-lsh</span>
total 321M
&nbsp;
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000000
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000001
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000002
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000003
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000004
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000005
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:02 000000010000000000000006
8.0K -rw——- <span style="color: #000000;">1</span> postgres postgres <span style="color: #000000;">254</span> <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:02 000000010000000000000006.00BA9328.backup
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:06 000000010000000000000007
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:07 000000010000000000000008
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:07 000000010000000000000009
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:07 00000001000000000000000A
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:07 00000001000000000000000B
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:07 00000001000000000000000C
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">14</span> 00000001000000000000000D
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">14</span> 00000001000000000000000E
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">14</span> 00000001000000000000000F
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">14</span> 000000010000000000000010
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">14</span> 000000010000000000000011
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">15</span> 000000010000000000000012
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">15</span> 000000010000000000000013</pre></div></div>

<p><b>4) Table testPITR4 created at 2008-11-25 20:00:04</b> &#8211;prepare for Point-in-time Recovery</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># psql</span>
<span style="color: #666666; font-style: italic;"># create table testPITR4 as select * from pg_class, pg_description; </span>
<span style="color: #666666; font-style: italic;"># select * from current_timestamp; –-2008-11-25 20:00:04</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>localhost pg_xlog<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># ls -lsh</span>
total 129M
8.0K -rw——- <span style="color: #000000;">1</span> postgres postgres <span style="color: #000000;">254</span> <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:02 000000010000000000000006.00BA9328.backup
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">19</span>:<span style="color: #000000;">59</span> 000000010000000000000017
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">19</span>:<span style="color: #000000;">59</span> 000000010000000000000018
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">19</span>:<span style="color: #000000;">59</span> 000000010000000000000019
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">20</span>:01 00000001000000000000001A
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">15</span> 00000001000000000000001B
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">19</span>:<span style="color: #000000;">58</span> 00000001000000000000001C
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">19</span>:<span style="color: #000000;">58</span> 00000001000000000000001D
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">19</span>:<span style="color: #000000;">59</span> 00000001000000000000001E
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>localhost wals<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># ls -lsh</span>
total 417M
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000000
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000001
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000002
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000003
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000004
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000010000000000000005
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:02 000000010000000000000006
8.0K -rw——- <span style="color: #000000;">1</span> postgres postgres <span style="color: #000000;">254</span> <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:02 000000010000000000000006.00BA9328.backup
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:06 000000010000000000000007
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:07 000000010000000000000008
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:07 000000010000000000000009
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:07 00000001000000000000000A
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:07 00000001000000000000000B
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:07 00000001000000000000000C
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">14</span> 00000001000000000000000D
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">14</span> 00000001000000000000000E
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">14</span> 00000001000000000000000F
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">14</span> 000000010000000000000010
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">14</span> 000000010000000000000011
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">15</span> 000000010000000000000012
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">18</span>:<span style="color: #000000;">15</span> 000000010000000000000013
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">19</span>:<span style="color: #000000;">58</span> 000000010000000000000014
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">19</span>:<span style="color: #000000;">58</span> 000000010000000000000015
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">19</span>:<span style="color: #000000;">59</span> 000000010000000000000016
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">19</span>:<span style="color: #000000;">59</span> 000000010000000000000017
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">19</span>:<span style="color: #000000;">59</span> 000000010000000000000018
17M -rw——- <span style="color: #000000;">1</span> postgres postgres 16M <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">19</span>:<span style="color: #000000;">59</span> 000000010000000000000019</pre></div></div>

<p><b>We created 4 tables for PITR recovery</b>, remember table creation time, we going to do recovery base on the time later.</p>

<div class="wp_syntax"><div class="code"><pre class="unix" style="font-family:monospace;">Table testPITR1 created at 2008-11-25 17:17
Table testPITR2 created at 2008-11-25 18:08:06
Table testPITR3 created at 2008-11-25 18:15:23
Table testPITR4 created at 2008-11-25 20:00:04</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">postgres</span>=<span style="color: #666666; font-style: italic;"># \d</span>
List of relations
Schema <span style="color: #000000; font-weight: bold;">|</span> Name <span style="color: #000000; font-weight: bold;">|</span> Type <span style="color: #000000; font-weight: bold;">|</span> Owner
——–+———–+——-+———-
public <span style="color: #000000; font-weight: bold;">|</span> testpitr1 <span style="color: #000000; font-weight: bold;">|</span> table <span style="color: #000000; font-weight: bold;">|</span> postgres
public <span style="color: #000000; font-weight: bold;">|</span> testpitr2 <span style="color: #000000; font-weight: bold;">|</span> table <span style="color: #000000; font-weight: bold;">|</span> postgres
public <span style="color: #000000; font-weight: bold;">|</span> testpitr3 <span style="color: #000000; font-weight: bold;">|</span> table <span style="color: #000000; font-weight: bold;">|</span> postgres
public <span style="color: #000000; font-weight: bold;">|</span> testpitr4 <span style="color: #000000; font-weight: bold;">|</span> table <span style="color: #000000; font-weight: bold;">|</span> postgres
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">4</span> rows<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p><b><font color=red>Attention!!! </font></b> Before move on, please study above transaction log files movement that generated by PostgreSQL. We have to fully understand when PostgreSQL will create a new log file and when it will move to archive folder, do not forget the log file format <img src='http://www.mkyong.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ~ take sometime to review and understand the above log file generation sequence</p>
<p><u><br />
<h2>Disaster come in </h2>
<p></u><br />
We have to do something in order to make our PostgreSQL server go down.</p>
<p>1) Kill the postgresql process</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #660033;">-9</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">head</span> <span style="color: #660033;">-1</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>pgDataPITR<span style="color: #000000; font-weight: bold;">/</span>postmaster.pid<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>2) Just turn off your Power <img src='http://www.mkyong.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><u><br />
<h2>Recovery Process</h2>
<p></u><br />
Finally we reach recovery process, Please remember 1 file and 2 folders </p>
<p><b>a) Base backup file</b> &#8211; pgdatabk20081125.tar<br />
<b>b) Log files hanv&#8217;t archive yet</b> &#8211; all files under Pg_xlog folder<br />
<b>c) WALs</b> &#8211; all archive files under wals folder (may be a remote storage in real environment)</p>
<p><b><font color=blue>Remember!!! </font></b>Assume we already backup <b>pgdatabk20081125.tar</b> and all archived files to pgbackup folder under <b>/usr/local/pgsql/pgbackup</b></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>pgbackup<span style="color: #000000; font-weight: bold;">/</span>pgbackup
$ <span style="color: #c20cb9; font-weight: bold;">mv</span> pgdatabk20081125.tar <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>pgbackup<span style="color: #000000; font-weight: bold;">/</span>pgbackup
$ <span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #660033;">-r</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>pgDataPITR<span style="color: #000000; font-weight: bold;">/</span>wals <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>pgbackup<span style="color: #000000; font-weight: bold;">/</span>pgbackup</pre></div></div>

<p><b>1) Rename pgDataPITR to pgDataPITR.bad.data</b>, assume database file in pgDataPITR folder was damaged due to disaster we created just now, we need to create a fresh database later.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">mv</span> pgDataPITR pgDataPITR.bad.data</pre></div></div>

<p><b>2) Unzip / extract files from pgdatabk20081125.tar</b>, create a new <b>pgDataPITR</b> folder under <b>/usr/local/pgdata/</b>, it just like what we did before. Move all extracted files from <b>pgdatabk20081125.tar</b> to <b>/usr/local/pgsql/pgDataPITR</b>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xvzf</span> pgdatabk20081125.tar 
<span style="color: #7a0874; font-weight: bold;">&#91;</span>extract-path<span style="color: #000000; font-weight: bold;">/</span>pgDataPITR<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>pgDataPITR</pre></div></div>

<p>Start database</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>pg_ctl start <span style="color: #660033;">-D</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>pgDataPITR<span style="color: #000000; font-weight: bold;">/</span>
$ psql
&nbsp;
<span style="color: #007800;">postgres</span>=<span style="color: #666666; font-style: italic;"># \d</span>
List of relations
Schema <span style="color: #000000; font-weight: bold;">|</span> Name <span style="color: #000000; font-weight: bold;">|</span> Type <span style="color: #000000; font-weight: bold;">|</span> Owner
——–+———–+——-+———-
public <span style="color: #000000; font-weight: bold;">|</span> testpitr1 <span style="color: #000000; font-weight: bold;">|</span> table <span style="color: #000000; font-weight: bold;">|</span> postgres</pre></div></div>

<p>Table testPITR1 created at 2008-11-25 17:17 is restored. This testPITR1 table is created before base backup process launched, so this is correct. </p>
<p><b>3) Copy log files from pg_xlog folder</b>. Some log files still located in pgDataPITR.bad.data pg_xlog folder (those log files hanv&#8217;t archive yet) during disaster happening, we need to copy the log file back and recover it as much as possible.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"> <span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>pgDataPITR.bad.data<span style="color: #000000; font-weight: bold;">/</span>pg_xlog<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span><span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>pgDataPITR</pre></div></div>

<p><b>4) Create a recovery.conf file and put it under /usr/local/pgsql/pgDataPITR</b></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">vi</span> recovery.conf</pre></div></div>

<p>Create following content in <b>recovery.conf </b></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">restore_command = <span style="color: #ff0000;">'cp /usr/local/pgsql/pgbackup/wals/%f %p'</span>
recovery_target_time = <span style="color: #ff0000;">'2008-11-25 18:08:06'</span></pre></div></div>

<p><b><font color=red>Attention !!!</font> This is the final process and most critical backup process</b></p>
<p><b>a) /usr/local/pgsql/pgbackup/wals/ </b>is the folder that we backup our archive log files<br />
<b>b) recovery_target_time </b>is the time we need to recover to. Omit this setting will make PostgreSQL recover as much as it can, it may recover all changes.</p>

<div class="wp_syntax"><div class="code"><pre class="unix" style="font-family:monospace;">Remember four tables creation time
Table testPITR1 created at 2008-11-25 17:17:00
Table testPITR2 created at 2008-11-25 18:08:06
Table testPITR3 created at 2008-11-25 18:15:23
Table testPITR4 created at 2008-11-25 20:00:04</pre></div></div>

<p><b><font color=red>Remember !!!</font></b> Above recovery.conf file will make PostgreSQL take the archive log files from <b>/usr/local/pgsql/pgbackup/wals/</b> folder and recover the data changes until <b>2008-11-25 18:08:06</b> (table testPITR2 created).</p>
<p><b>5) Start database and output log file to /usr/local/pgsql/pgDataPITR/pg.log</b></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ pg_ctl start <span style="color: #660033;">-D</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>pgDataPITR <span style="color: #660033;">-l</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>pgDataPITR<span style="color: #000000; font-weight: bold;">/</span>pg.log
$ psql
<span style="color: #007800;">postgres</span>=<span style="color: #666666; font-style: italic;"># \d</span>
           List of relations
 Schema <span style="color: #000000; font-weight: bold;">|</span>   Name    <span style="color: #000000; font-weight: bold;">|</span> Type  <span style="color: #000000; font-weight: bold;">|</span>  Owner
--------+-----------+-------+----------
 public <span style="color: #000000; font-weight: bold;">|</span> testpitr1 <span style="color: #000000; font-weight: bold;">|</span> table <span style="color: #000000; font-weight: bold;">|</span> postgres
 public <span style="color: #000000; font-weight: bold;">|</span> testpitr2 <span style="color: #000000; font-weight: bold;">|</span> table <span style="color: #000000; font-weight: bold;">|</span> postgres
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">2</span> rows<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>Table testpitr2 is restored back.</p>
<p>P.S After recovery process finished, recovery.conf will rename to recovery.done by PostgreSQL to avoid start the recovery process again.</p>
<p>We can the view pg.log file to understand how PostgreSQL process the recovery process.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>postgres<span style="color: #000000; font-weight: bold;">@</span>localhost pgDataPITR<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">cat</span> pg.log
STATEMENT:  <span style="color: #000000; font-weight: bold;">select</span> pg_start_backup<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;Full Backup - Testing&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
LOG:  database system was interrupted; <span style="color: #c20cb9; font-weight: bold;">last</span> known up at <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">45</span>:<span style="color: #000000;">23</span> MYT
LOG:  starting archive recovery
LOG:  restore_command = <span style="color: #ff0000;">'cp /usr/local/pgsql/pgbackup/wals/%f %p'</span>
LOG:  recovery_target_time = <span style="color: #ff0000;">'2008-11-25 18:08:06+08'</span>
<span style="color: #c20cb9; font-weight: bold;">cp</span>: cannot <span style="color: #c20cb9; font-weight: bold;">stat</span> <span style="color: #000000; font-weight: bold;">`/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>pgsql<span style="color: #000000; font-weight: bold;">/</span>pgbackup<span style="color: #000000; font-weight: bold;">/</span>wals<span style="color: #000000; font-weight: bold;">/</span>00000001.history<span style="color: #ff0000;">': No such file or directory
LOG:  restored log file &quot;000000010000000000000006.00BA9328.backup&quot; from archive
LOG:  restored log file &quot;000000010000000000000006&quot; from archive
LOG:  automatic recovery in progress
LOG:  redo starts at 0/6BA9368
LOG:  restored log file &quot;000000010000000000000007&quot; from archive
LOG:  restored log file &quot;000000010000000000000008&quot; from archive
LOG:  restored log file &quot;000000010000000000000009&quot; from archive
LOG:  restored log file &quot;00000001000000000000000A&quot; from archive
LOG:  restored log file &quot;00000001000000000000000B&quot; from archive
LOG:  restored log file &quot;00000001000000000000000C&quot; from archive
LOG:  restored log file &quot;00000001000000000000000D&quot; from archive
LOG:  recovery stopping before commit of transaction 395, time 2008-11-25 18:08:34.180397+08
LOG:  redo done at 0/D85E0FC
LOG:  last completed transaction was at log time 2008-11-25 18:08:34.180397+08</span></pre></div></div>

<p> <b><font color=red>Attention !!!</font><font color=red>Attention !!!</font></b> THIS IS ONE TIME PROCESS, after recovery process started and finished, we cant make any recovery changes (like roll forward to another time). </p>
<p>If we want to roll forward to another restore time, we need to start whole recovery process again, like extract files from base backup and copy log files. This is because after PostgreSQL recovered the data , all log files format will changed to other format like following</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>postgres<span style="color: #000000; font-weight: bold;">@</span>localhost pgDataPITR<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #7a0874; font-weight: bold;">cd</span> pg_xlog<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>postgres<span style="color: #000000; font-weight: bold;">@</span>localhost pg_xlog<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-ls</span>
total <span style="color: #000000;">147696</span>
<span style="color: #000000;">16408</span> <span style="color: #660033;">-rw-------</span> <span style="color: #000000;">1</span> postgres postgres <span style="color: #000000;">16777216</span> <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">26</span> <span style="color: #000000;">14</span>:<span style="color: #000000;">28</span> 00000002000000000000000D
    <span style="color: #000000;">8</span> <span style="color: #660033;">-rw-------</span> <span style="color: #000000;">1</span> postgres postgres       <span style="color: #000000;">83</span> <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">26</span> <span style="color: #000000;">14</span>:<span style="color: #000000;">22</span> 00000002.history
<span style="color: #000000;">16408</span> <span style="color: #660033;">-rw-------</span> <span style="color: #000000;">1</span> postgres postgres <span style="color: #000000;">16777216</span> <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">26</span> <span style="color: #000000;">14</span>:<span style="color: #000000;">30</span> 00000003000000000000000D
<span style="color: #000000;">16408</span> <span style="color: #660033;">-rw-------</span> <span style="color: #000000;">1</span> postgres postgres <span style="color: #000000;">16777216</span> <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">16</span> 00000003000000000000000E
<span style="color: #000000;">16408</span> <span style="color: #660033;">-rw-------</span> <span style="color: #000000;">1</span> postgres postgres <span style="color: #000000;">16777216</span> <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 00000003000000000000000F
<span style="color: #000000;">16408</span> <span style="color: #660033;">-rw-------</span> <span style="color: #000000;">1</span> postgres postgres <span style="color: #000000;">16777216</span> <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000030000000000000010
<span style="color: #000000;">16408</span> <span style="color: #660033;">-rw-------</span> <span style="color: #000000;">1</span> postgres postgres <span style="color: #000000;">16777216</span> <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">45</span> 000000030000000000000011
<span style="color: #000000;">16408</span> <span style="color: #660033;">-rw-------</span> <span style="color: #000000;">1</span> postgres postgres <span style="color: #000000;">16777216</span> <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000030000000000000012
<span style="color: #000000;">16408</span> <span style="color: #660033;">-rw-------</span> <span style="color: #000000;">1</span> postgres postgres <span style="color: #000000;">16777216</span> <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000030000000000000013
<span style="color: #000000;">16408</span> <span style="color: #660033;">-rw-------</span> <span style="color: #000000;">1</span> postgres postgres <span style="color: #000000;">16777216</span> <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">25</span> <span style="color: #000000;">17</span>:<span style="color: #000000;">17</span> 000000030000000000000014
    <span style="color: #000000;">8</span> <span style="color: #660033;">-rw-------</span> <span style="color: #000000;">1</span> postgres postgres      <span style="color: #000000;">158</span> <span style="color: #000000;">2008</span>-<span style="color: #000000;">11</span>-<span style="color: #000000;">26</span> <span style="color: #000000;">14</span>:<span style="color: #000000;">30</span> 00000003.history</pre></div></div>

<p>After recovered, log file number will increased<br />
00000001 &#8211;> 00000002 &#8211;> 00000003</p>
<p>If we want to <b>restore table testPITR3 created at 2008-11-25 18:15:23</b>, we are unable to do it, it will output error in log file unless we start the whole recovery process again.</p>
<p><u><br />
<h2>Conclusion</h2>
<p></u><br />
This archive log files transaction backup and restore mechanism is implemented in many enterprise database like Oracle. The archive log files backup concept is very important, please do some real practice and master it, it really help when your database went down.</p>
<p>Thanks for reading this long article, please correct me if i write something wrong <img src='http://www.mkyong.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  hope help.</p>
<p><b>PostgreSQL Official Point-in-time Recovery Reference</b><br />
<a href="http://www.postgresql.org/docs/8.0/interactive/backup-online.html" target="_blank">http://www.postgresql.org/docs/8.0/interactive/backup-online.html</a></p>
<ul class="related_post"><li><a href="http://www.mkyong.com/database/regular-expression-in-postgresql/" title="Regular Expression in PostgreSQL">Regular Expression in PostgreSQL</a></li><li><a href="http://www.mkyong.com/blog/how-do-backup-wordpress-database-phpmyadmin/" title="How to backup wordpress database &#8211; phpMyAdmin">How to backup wordpress database &#8211; phpMyAdmin</a></li><li><a href="http://www.mkyong.com/java/how-do-connect-to-postgresql-with-jdbc-driver-java/" title="How to connect to PostgreSQL with JDBC driver &#8211; Java">How to connect to PostgreSQL with JDBC driver &#8211; Java</a></li><li><a href="http://www.mkyong.com/database/how-to-export-table-data-to-file-csv-postgresql/" title="How to export table data to file / csv &#8211; PostgreSQL">How to export table data to file / csv &#8211; PostgreSQL</a></li><li><a href="http://www.mkyong.com/database/how-to-kill-terminate-postgresql-hang-query/" title="How to kill / terminate PostgreSQL hang query">How to kill / terminate PostgreSQL hang query</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mkyong.com/database/postgresql-point-in-time-recovery-incremental-backup/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to export table data to file / csv &#8211; PostgreSQL</title>
		<link>http://www.mkyong.com/database/how-to-export-table-data-to-file-csv-postgresql/</link>
		<comments>http://www.mkyong.com/database/how-to-export-table-data-to-file-csv-postgresql/#comments</comments>
		<pubDate>Tue, 09 Sep 2008 03:03:46 +0000</pubDate>
		<dc:creator>mkyong</dc:creator>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[export]]></category>
		<category><![CDATA[export file]]></category>
		<category><![CDATA[postgresql]]></category>

		<guid isPermaLink="false">http://www.mkyong.com/?p=188</guid>
		<description><![CDATA[My boss ask me export some data from database for him to do statistic report. PostgreSQL provided easy and useful export feature to do it. Here i will demonstrate how to export data or query result from PostgreSQL into a file or csv file.
1) Connect to PostgreSQL with psql command

yongmo@abcdb:~$ psql -p 5433 -U dba [...]]]></description>
			<content:encoded><![CDATA[<p>My boss ask me export some data from database for him to do statistic report. PostgreSQL provided easy and useful export feature to do it. Here i will demonstrate how to export data or query result from PostgreSQL into a file or csv file.</p>
<p>1) Connect to PostgreSQL with psql command</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">yongmo<span style="color: #000000; font-weight: bold;">@</span>abcdb:~$ psql <span style="color: #660033;">-p</span> <span style="color: #000000;">5433</span> <span style="color: #660033;">-U</span> dba dbname</pre></div></div>

<p>P.S i installed my PostgreSQL in port 5433, so i need to specific port number.<br />
P.S Type \? to view all available command</p>
<p>2) Type <b>\o /home/yongmo/data25000.csv</b>, it will export query result to /home/yongmo/data25000.csv file.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">dbname</span>=<span style="color: #000000; font-weight: bold;">&gt;</span> \o <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>yongmo<span style="color: #000000; font-weight: bold;">/</span>data25000.csv</pre></div></div>

<p>3) Type query that you want to export</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">dbname</span>=<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">select</span> url from urltable where <span style="color: #007800;">scoreid</span>=<span style="color: #000000;">1</span> limit <span style="color: #000000;">25000</span>;</pre></div></div>

<p>4) Done, all query result exported to /home/yongmo/data25000.csv</p>
<p>Here is the full command</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">yongmo<span style="color: #000000; font-weight: bold;">@</span>abcdb:~$ psql <span style="color: #660033;">-p</span> <span style="color: #000000;">5433</span> <span style="color: #660033;">-U</span> dba dbname
Password <span style="color: #000000; font-weight: bold;">for</span> user dba: 
Welcome to psql 8.2.4 <span style="color: #7a0874; font-weight: bold;">&#40;</span>server 8.3.3<span style="color: #7a0874; font-weight: bold;">&#41;</span>, the PostgreSQL interactive terminal.
&nbsp;
Type:  \copyright <span style="color: #000000; font-weight: bold;">for</span> distribution terms
       \h <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #7a0874; font-weight: bold;">help</span> with SQL commands
       \? <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #7a0874; font-weight: bold;">help</span> with psql commands
       \g or terminate with semicolon to execute query
       \q to quit
&nbsp;
WARNING:  You are connected to a server with major version <span style="color: #000000;">8.3</span>,
but your psql client is major version 8.2.  Some backslash commands,
such <span style="color: #c20cb9; font-weight: bold;">as</span> \d, might not work properly.
&nbsp;
<span style="color: #007800;">dbname</span>=<span style="color: #000000; font-weight: bold;">&gt;</span> \o <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>yongmo<span style="color: #000000; font-weight: bold;">/</span>data25000.csv
<span style="color: #007800;">dbname</span>=<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">select</span> url from urltable where <span style="color: #007800;">scoreid</span>=<span style="color: #000000;">1</span> limit <span style="color: #000000;">25000</span>;
<span style="color: #007800;">dbname</span>=<span style="color: #000000; font-weight: bold;">&gt;</span> \q</pre></div></div>

<ul class="related_post"><li><a href="http://www.mkyong.com/database/regular-expression-in-postgresql/" title="Regular Expression in PostgreSQL">Regular Expression in PostgreSQL</a></li><li><a href="http://www.mkyong.com/database/to_date-function-between-postgresql-82-and-83/" title="TO_DATE function between PostgreSQL 8.2 and 8.3">TO_DATE function between PostgreSQL 8.2 and 8.3</a></li><li><a href="http://www.mkyong.com/database/backup-restore-database-in-postgresql-pg_dumppg_restore/" title="Backup &#038; Restore Database in PostgreSQL (pg_dump,pg_restore)">Backup &#038; Restore Database in PostgreSQL (pg_dump,pg_restore)</a></li><li><a href="http://www.mkyong.com/database/postgresql-error-operator-does-not-exist-smallint-character-varying-solution/" title="PostgreSQL &#8211; ERROR: operator does not exist: smallint = character varying (Solution)">PostgreSQL &#8211; ERROR: operator does not exist: smallint = character varying (Solution)</a></li><li><a href="http://www.mkyong.com/database/install-perl-in-postgresql-the-specified-module-could-not-be-found/" title="Install Perl in PostgreSQL, The specified module could not be found.">Install Perl in PostgreSQL, The specified module could not be found.</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mkyong.com/database/how-to-export-table-data-to-file-csv-postgresql/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>How to kill / terminate PostgreSQL hang query</title>
		<link>http://www.mkyong.com/database/how-to-kill-terminate-postgresql-hang-query/</link>
		<comments>http://www.mkyong.com/database/how-to-kill-terminate-postgresql-hang-query/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 06:30:40 +0000</pubDate>
		<dc:creator>mkyong</dc:creator>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[hang query]]></category>
		<category><![CDATA[kill]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[postgresql]]></category>

		<guid isPermaLink="false">http://www.mkyong.com/?p=179</guid>
		<description><![CDATA[Today i find out one of the query is hanging in PostgreSQL, it can&#8217;t release itself, it just seem like stuck in PostgreSQL forever.
I issue a command to cancel it in PostgreSQL, but it seem not working. Query just hanging there and show an &#8220;idle in transaction&#8221; status. I have no way to terminal it, [...]]]></description>
			<content:encoded><![CDATA[<p>Today i find out one of the query is hanging in PostgreSQL, it can&#8217;t release itself, it just seem like stuck in PostgreSQL forever.</p>
<p>I issue a command to cancel it in PostgreSQL, but it seem not working. Query just hanging there and show an &#8220;idle in transaction&#8221; status. I have no way to terminal it, it left me no choice but go Debian terminal to issue kill command to terminate it manually. </p>
<p>Query hanging or not responding in PostgreSQL is because i didn&#8217;t handle transaction manager properly in my web application. When System accidently shut down, running query will hanging in PostgreSQL and my transaction manager (DataSourceTransactionManager) is not manage to rollback whole transaction. I have no idea why it&#8217;s not working. After i changed my transaction manager to JtaTransactionManager, it work correctly and no transaction hanging in middle way when system accidentally shut down.</p>
<p>However here i show how to terminal the hanging query in PostgreSQL. The hanging query will display as <b>&#8220;idle in transaction&#8221; </b> in PostgreSQL. I have to list out all PostgreSQL processes and issue a kill terminate command to terminate the query manually.</p>
<p>1) Issue <b>ps -ef | grep postgres</b> to list out all processes belong to postgres user.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mkyong:~<span style="color: #666666; font-style: italic;"># ps -ef | grep postgres</span>
postgres <span style="color: #000000;">13648</span>     <span style="color: #000000;">1</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:04 ?        00:00:00 <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>postgresql<span style="color: #000000; font-weight: bold;">/</span>PostgresPlus8.3xxxxxxx
postgres <span style="color: #000000;">13651</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:04 ?        00:00:00 postgres: logger process                                                                        
postgres <span style="color: #000000;">13653</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:04 ?        00:00:00 postgres: writer process                                                                        
postgres <span style="color: #000000;">13654</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:04 ?        00:00:00 postgres: wal writer process                                                                    
postgres <span style="color: #000000;">13655</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:04 ?        00:00:00 postgres: autovacuum launcher process                                                           
postgres <span style="color: #000000;">13656</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:04 ?        00:00:00 postgres: stats collector process                                                               
postgres <span style="color: #000000;">13668</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:04 ?        00:00:00 postgres: postgres postgres 10.70.1.27<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">3734</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                               
postgres <span style="color: #000000;">13689</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:05 ?        00:00:00 postgres: usrdba db_test 10.70.1.67<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">4164</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                               
postgres <span style="color: #000000;">13714</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:06 ?        00:00:00 postgres: usrdba db_test 10.70.0.61<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">57586</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle <span style="color: #000000; font-weight: bold;">in</span> transaction                               
postgres <span style="color: #000000;">13721</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:06 ?        00:00:<span style="color: #000000;">16</span> postgres: usrdba db_test 10.70.1.67<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">4165</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                               
postgres <span style="color: #000000;">13832</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:<span style="color: #000000;">10</span> ?        00:00:00 postgres: usrdba db_test 10.70.0.61<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">57592</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                              
postgres <span style="color: #000000;">13833</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:<span style="color: #000000;">10</span> ?        00:00:00 postgres: usrdba db_test 10.70.0.61<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">57593</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                              
postgres <span style="color: #000000;">13834</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:<span style="color: #000000;">10</span> ?        00:00:00 postgres: usrdba db_test 10.70.0.61<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">57594</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                              
postgres <span style="color: #000000;">13835</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:<span style="color: #000000;">10</span> ?        00:00:00 postgres: usrdba db_test 10.70.0.61<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">57595</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                              
postgres <span style="color: #000000;">13836</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:<span style="color: #000000;">10</span> ?        00:00:00 postgres: usrdba db_test 10.70.0.61<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">57596</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                              
postgres <span style="color: #000000;">14201</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:<span style="color: #000000;">23</span> ?        00:00:00 postgres: nrsdba postgres 10.70.1.8<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">4419</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                                  
postgres <span style="color: #000000;">14202</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:<span style="color: #000000;">23</span> ?        00:00:00 postgres: usrdba db_test 10.70.1.8<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">4420</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                                
postgres <span style="color: #000000;">14207</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:<span style="color: #000000;">24</span> ?        00:00:00 postgres: usrdba db_test 10.70.1.8<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">4421</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                                
root     <span style="color: #000000;">18030</span> <span style="color: #000000;">17992</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">13</span>:<span style="color: #000000;">46</span> pts<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>    00:00:00 <span style="color: #c20cb9; font-weight: bold;">grep</span> postgres
mkyong:~<span style="color: #666666; font-style: italic;">#</span></pre></div></div>

<p>2) <b>Notice process id 13714, idle in transaction</b>, this is the hanging query in PostgreSQL. Issue following command to terminate the PostgreSQL process manually.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mkyong:~<span style="color: #666666; font-style: italic;"># kill 13714</span></pre></div></div>

<p>or</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mkyong:~<span style="color: #666666; font-style: italic;"># kill -TERM 13714</span></pre></div></div>

<p>or</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mkyong:~<span style="color: #666666; font-style: italic;"># kill -15 13714</span></pre></div></div>

<p>3) Done, hanging query is gone</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mkyong:~<span style="color: #666666; font-style: italic;"># ps -ef | grep postgres</span>
postgres <span style="color: #000000;">13648</span>     <span style="color: #000000;">1</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:04 ?        00:00:00 <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>postgresql<span style="color: #000000; font-weight: bold;">/</span>PostgresPlus8.3xxxxxxx
postgres <span style="color: #000000;">13651</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:04 ?        00:00:00 postgres: logger process                                                                        
postgres <span style="color: #000000;">13653</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:04 ?        00:00:00 postgres: writer process                                                                        
postgres <span style="color: #000000;">13654</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:04 ?        00:00:00 postgres: wal writer process                                                                    
postgres <span style="color: #000000;">13655</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:04 ?        00:00:00 postgres: autovacuum launcher process                                                           
postgres <span style="color: #000000;">13656</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:04 ?        00:00:00 postgres: stats collector process                                                               
postgres <span style="color: #000000;">13668</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:04 ?        00:00:00 postgres: postgres postgres 10.70.1.27<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">3734</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                               
postgres <span style="color: #000000;">13689</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:05 ?        00:00:00 postgres: usrdba db_test 10.70.1.67<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">4164</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                                                         
postgres <span style="color: #000000;">13721</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:06 ?        00:00:<span style="color: #000000;">16</span> postgres: usrdba db_test 10.70.1.67<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">4165</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                               
postgres <span style="color: #000000;">13832</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:<span style="color: #000000;">10</span> ?        00:00:00 postgres: usrdba db_test 10.70.0.61<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">57592</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                              
postgres <span style="color: #000000;">13833</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:<span style="color: #000000;">10</span> ?        00:00:00 postgres: usrdba db_test 10.70.0.61<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">57593</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                              
postgres <span style="color: #000000;">13834</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:<span style="color: #000000;">10</span> ?        00:00:00 postgres: usrdba db_test 10.70.0.61<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">57594</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                              
postgres <span style="color: #000000;">13835</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:<span style="color: #000000;">10</span> ?        00:00:00 postgres: usrdba db_test 10.70.0.61<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">57595</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                              
postgres <span style="color: #000000;">13836</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:<span style="color: #000000;">10</span> ?        00:00:00 postgres: usrdba db_test 10.70.0.61<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">57596</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                              
postgres <span style="color: #000000;">14201</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:<span style="color: #000000;">23</span> ?        00:00:00 postgres: nrsdba postgres 10.70.1.8<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">4419</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                                  
postgres <span style="color: #000000;">14202</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:<span style="color: #000000;">23</span> ?        00:00:00 postgres: usrdba db_test 10.70.1.8<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">4420</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                                
postgres <span style="color: #000000;">14207</span> <span style="color: #000000;">13648</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">11</span>:<span style="color: #000000;">24</span> ?        00:00:00 postgres: usrdba db_test 10.70.1.8<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">4421</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> idle                                                
root     <span style="color: #000000;">18030</span> <span style="color: #000000;">17992</span>  <span style="color: #000000;">0</span> <span style="color: #000000;">13</span>:<span style="color: #000000;">46</span> pts<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>    00:00:00 <span style="color: #c20cb9; font-weight: bold;">grep</span> postgres
mkyong:~<span style="color: #666666; font-style: italic;">#</span></pre></div></div>

<ul class="related_post"><li><a href="http://www.mkyong.com/database/regular-expression-in-postgresql/" title="Regular Expression in PostgreSQL">Regular Expression in PostgreSQL</a></li><li><a href="http://www.mkyong.com/database/postgresql-error-operator-does-not-exist-smallint-character-varying-solution/" title="PostgreSQL &#8211; ERROR: operator does not exist: smallint = character varying (Solution)">PostgreSQL &#8211; ERROR: operator does not exist: smallint = character varying (Solution)</a></li><li><a href="http://www.mkyong.com/database/performance-testing-on-partition-table-in-postgresql-part-3/" title="Performance Testing on Partition Table In PostgreSQL &#8211; Part 3">Performance Testing on Partition Table In PostgreSQL &#8211; Part 3</a></li><li><a href="http://www.mkyong.com/database/how-to-install-pgagent-on-windows-postgresql-job-scheduler/" title="How to install pgAgent on windows (PostgreSQL Job Scheduler)">How to install pgAgent on windows (PostgreSQL Job Scheduler)</a></li><li><a href="http://www.mkyong.com/database/how-to-compile-postgresql-database-source-code-in-linux/" title="How to compile PostgreSQL database source code in linux">How to compile PostgreSQL database source code in linux</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mkyong.com/database/how-to-kill-terminate-postgresql-hang-query/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TO_DATE function between PostgreSQL 8.2 and 8.3</title>
		<link>http://www.mkyong.com/database/to_date-function-between-postgresql-82-and-83/</link>
		<comments>http://www.mkyong.com/database/to_date-function-between-postgresql-82-and-83/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 07:07:23 +0000</pubDate>
		<dc:creator>mkyong</dc:creator>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[migration problem]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[to_date]]></category>

		<guid isPermaLink="false">http://www.mkyong.com/?p=174</guid>
		<description><![CDATA[As we all know PostgreSQL 8.3 make very strong checking on data type, it make a lot application hit many data type error after migration from PostgreSQL8.x to PostgreSQL8.3. Mostly is cause by data type checking.
One of the common error is to_date() function. It accept two text as parameters.

to_date&#40;text,text&#41;

Ok now i create a table as [...]]]></description>
			<content:encoded><![CDATA[<p>As we all know PostgreSQL 8.3 make very strong checking on data type, it make a lot application hit many data type error after migration from PostgreSQL8.x to PostgreSQL8.3. Mostly is cause by data type checking.</p>
<p>One of the common error is to_date() function. It accept two text as parameters.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">to_date<span style="color: #66cc66;">&#40;</span>text<span style="color: #66cc66;">,</span>text<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Ok now i create a table as following. A simple table call n_url_test contain a createddate as timestamp data type.</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;">TABLE</span> n_url_test
<span style="color: #66cc66;">&#40;</span>
  urltestid bigserial <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  createddate timestamp without time zone <span style="color: #993333; font-weight: bold;">DEFAULT</span> now<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  CONSTRAINT n_url_to_test_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>urltestid<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>I run following sql in PostgreSQL8.2, it return my expected result as &#8216;YYYY-MM-DD&#8217; format.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">--PostgreSQL 8.2</span>
<span style="color: #808080; font-style: italic;">---------------</span>
<span style="color: #993333; font-weight: bold;">SELECT</span>  to_date<span style="color: #66cc66;">&#40;</span>createddate<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'YYYY-MM-DD'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> n_url_test;</pre></div></div>

<p>However after i migrated to PostgreSQL8.3, i hit following error</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">--PostgreSQL 8.3</span>
<span style="color: #808080; font-style: italic;">---------------</span>
<span style="color: #993333; font-weight: bold;">SELECT</span>  to_date<span style="color: #66cc66;">&#40;</span>createddate<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'YYYY-MM-DD'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> n_url_test;
&nbsp;
<span style="color: #808080; font-style: italic;">--------------------------------------------------------------------------------</span>
ERROR:  <span style="color: #993333; font-weight: bold;">FUNCTION</span> to_date<span style="color: #66cc66;">&#40;</span>timestamp without time zone<span style="color: #66cc66;">,</span> unknown<span style="color: #66cc66;">&#41;</span> does <span style="color: #993333; font-weight: bold;">NOT</span> exist
LINE <span style="color: #cc66cc;">1</span>: <span style="color: #993333; font-weight: bold;">SELECT</span>  to_date<span style="color: #66cc66;">&#40;</span>createddate<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'YYYY-MM-DD'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> n_url_test
                ^
HINT:  No <span style="color: #993333; font-weight: bold;">FUNCTION</span> matches the given name <span style="color: #993333; font-weight: bold;">AND</span> argument types<span style="color: #66cc66;">.</span> 
You might need <span style="color: #993333; font-weight: bold;">TO</span> <span style="color: #993333; font-weight: bold;">ADD</span> explicit type casts<span style="color: #66cc66;">.</span>
&nbsp;
<span style="color: #66cc66;">**********</span> Error <span style="color: #66cc66;">**********</span></pre></div></div>

<p>PostgreSQL 8.3 will not automatically convert from timestamp to text for us like what it does in previous version. We need to explicit type casts createddate like following</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">--PostgreSQL 8.3</span>
<span style="color: #808080; font-style: italic;">---------------</span>
<span style="color: #993333; font-weight: bold;">SELECT</span>  to_date<span style="color: #66cc66;">&#40;</span>createddate::text<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'YYYY-MM-DD'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> n_url_test;</pre></div></div>

<p>Append <b>::text</b> to createddate will explicit convert datatype as text, It&#8217;s work. PostgreSQL8.3 strictly checking on data type is good function , but please do not forget there still have a lot people using previous version. It cause a lot problem if application is design base on ignore data type checking  like what i mention above.</p>
<ul class="related_post"><li><a href="http://www.mkyong.com/database/regular-expression-in-postgresql/" title="Regular Expression in PostgreSQL">Regular Expression in PostgreSQL</a></li><li><a href="http://www.mkyong.com/database/how-to-export-table-data-to-file-csv-postgresql/" title="How to export table data to file / csv &#8211; PostgreSQL">How to export table data to file / csv &#8211; PostgreSQL</a></li><li><a href="http://www.mkyong.com/database/backup-restore-database-in-postgresql-pg_dumppg_restore/" title="Backup &#038; Restore Database in PostgreSQL (pg_dump,pg_restore)">Backup &#038; Restore Database in PostgreSQL (pg_dump,pg_restore)</a></li><li><a href="http://www.mkyong.com/database/postgresql-error-operator-does-not-exist-smallint-character-varying-solution/" title="PostgreSQL &#8211; ERROR: operator does not exist: smallint = character varying (Solution)">PostgreSQL &#8211; ERROR: operator does not exist: smallint = character varying (Solution)</a></li><li><a href="http://www.mkyong.com/database/install-perl-in-postgresql-the-specified-module-could-not-be-found/" title="Install Perl in PostgreSQL, The specified module could not be found.">Install Perl in PostgreSQL, The specified module could not be found.</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mkyong.com/database/to_date-function-between-postgresql-82-and-83/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PostgreSQL &#8211; How to change default schema</title>
		<link>http://www.mkyong.com/database/postgresql-how-to-change-default-schema/</link>
		<comments>http://www.mkyong.com/database/postgresql-how-to-change-default-schema/#comments</comments>
		<pubDate>Mon, 18 Aug 2008 02:16:13 +0000</pubDate>
		<dc:creator>mkyong</dc:creator>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[default schema]]></category>
		<category><![CDATA[postgresql]]></category>

		<guid isPermaLink="false">http://www.mkyong.com/?p=169</guid>
		<description><![CDATA[“public” is PostgreSQL default scheme, i have to change it because i had migrated a new database data into another new schema call “new_public”.
Before start to change, i have to check what is current PostgreSQL default schema?
1) Command

SHOW search_path

2) Check postgresql.conf

#---------------------------------------------------------------------------
# CLIENT CONNECTION DEFAULTS
#---------------------------------------------------------------------------
&#160;
# - Statement Behavior -
&#160;
#search_path = '&#34;$user&#34;,public'		# schema names
#default_tablespace = ''		# a [...]]]></description>
			<content:encoded><![CDATA[<p>“public” is PostgreSQL default scheme, i have to change it because i had migrated a new database data into another new schema call “new_public”.<br />
Before start to change, i have to check what is current PostgreSQL default schema?</p>
<p>1) Command</p>

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

<p>2) Check postgresql.conf</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#---------------------------------------------------------------------------</span>
<span style="color: #808080; font-style: italic;"># CLIENT CONNECTION DEFAULTS</span>
<span style="color: #808080; font-style: italic;">#---------------------------------------------------------------------------</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># - Statement Behavior -</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#search_path = '&quot;$user&quot;,public'		# schema names</span>
<span style="color: #808080; font-style: italic;">#default_tablespace = ''		# a tablespace name, '' uses</span>
					<span style="color: #808080; font-style: italic;"># the default</span>
<span style="color: #808080; font-style: italic;">#check_function_bodies = on</span>
<span style="color: #808080; font-style: italic;">#default_transaction_isolation = 'read committed'</span>
<span style="color: #808080; font-style: italic;">#default_transaction_read_only = off</span></pre></div></div>

<p>Here i show how to change Postgresql default schema.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SET</span> search_path <span style="color: #66cc66;">=</span> new_schema</pre></div></div>

<p>However above command is apply to current session only, next time schema will change back to public. If we want to make effect permanently, we have to change in postgresql.conf file like following.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#---------------------------------------------------------------------------</span>
<span style="color: #808080; font-style: italic;"># CLIENT CONNECTION DEFAULTS</span>
<span style="color: #808080; font-style: italic;">#---------------------------------------------------------------------------</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># - Statement Behavior -</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#search_path = '&quot;$user&quot;,public'		# schema names</span>
search_path <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'&quot;$user&quot;,new_schema'</span>	<span style="color: #808080; font-style: italic;"># NEW SCHEMA HERE</span>
<span style="color: #808080; font-style: italic;">#default_tablespace = ''		# a tablespace name, '' uses</span>
					<span style="color: #808080; font-style: italic;"># the default</span>
<span style="color: #808080; font-style: italic;">#check_function_bodies = on</span>
<span style="color: #808080; font-style: italic;">#default_transaction_isolation = 'read committed'</span>
<span style="color: #808080; font-style: italic;">#default_transaction_read_only = off</span></pre></div></div>

<p>After that just restart PostgreSQL service. Done.</p>
<ul class="related_post"><li><a href="http://www.mkyong.com/database/regular-expression-in-postgresql/" title="Regular Expression in PostgreSQL">Regular Expression in PostgreSQL</a></li><li><a href="http://www.mkyong.com/java/how-do-connect-to-postgresql-with-jdbc-driver-java/" title="How to connect to PostgreSQL with JDBC driver &#8211; Java">How to connect to PostgreSQL with JDBC driver &#8211; Java</a></li><li><a href="http://www.mkyong.com/database/postgresql-point-in-time-recovery-incremental-backup/" title="PostgreSQL Point-in-time Recovery (Incremental Backup)">PostgreSQL Point-in-time Recovery (Incremental Backup)</a></li><li><a href="http://www.mkyong.com/database/how-to-export-table-data-to-file-csv-postgresql/" title="How to export table data to file / csv &#8211; PostgreSQL">How to export table data to file / csv &#8211; PostgreSQL</a></li><li><a href="http://www.mkyong.com/database/how-to-kill-terminate-postgresql-hang-query/" title="How to kill / terminate PostgreSQL hang query">How to kill / terminate PostgreSQL hang query</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mkyong.com/database/postgresql-how-to-change-default-schema/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to get random row from sql query &#8211; PostgreSQL</title>
		<link>http://www.mkyong.com/database/how-to-get-random-row-from-sql-query-postgresql/</link>
		<comments>http://www.mkyong.com/database/how-to-get-random-row-from-sql-query-postgresql/#comments</comments>
		<pubDate>Mon, 11 Aug 2008 09:58:48 +0000</pubDate>
		<dc:creator>mkyong</dc:creator>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[random row]]></category>
		<category><![CDATA[RANDOM()]]></category>

		<guid isPermaLink="false">http://www.mkyong.com/?p=166</guid>
		<description><![CDATA[How can i get a random row from a query result in PostgreSQL?
The answer is easy, just using RANDOM() built-in function in PostgreSQL

SELECT scoreid FROM n_score ORDER BY RANDOM&#40;&#41; LIMIT 1;

Above example will get a random score id from table n_score. Everytime execute it will get a random score id. Done.
Regular Expression in PostgreSQLHow to [...]]]></description>
			<content:encoded><![CDATA[<p><b>How can i get a random row from a query result in PostgreSQL?</b><br />
The answer is easy, just using RANDOM() built-in function in PostgreSQL</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> scoreid <span style="color: #993333; font-weight: bold;">FROM</span> n_score <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> RANDOM<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">LIMIT</span> <span style="color: #cc66cc;">1</span>;</pre></div></div>

<p>Above example will get a random score id from table n_score. Everytime execute it will get a random score id. Done.</p>
<ul class="related_post"><li><a href="http://www.mkyong.com/database/regular-expression-in-postgresql/" title="Regular Expression in PostgreSQL">Regular Expression in PostgreSQL</a></li><li><a href="http://www.mkyong.com/java/how-do-connect-to-postgresql-with-jdbc-driver-java/" title="How to connect to PostgreSQL with JDBC driver &#8211; Java">How to connect to PostgreSQL with JDBC driver &#8211; Java</a></li><li><a href="http://www.mkyong.com/database/postgresql-point-in-time-recovery-incremental-backup/" title="PostgreSQL Point-in-time Recovery (Incremental Backup)">PostgreSQL Point-in-time Recovery (Incremental Backup)</a></li><li><a href="http://www.mkyong.com/database/how-to-export-table-data-to-file-csv-postgresql/" title="How to export table data to file / csv &#8211; PostgreSQL">How to export table data to file / csv &#8211; PostgreSQL</a></li><li><a href="http://www.mkyong.com/database/how-to-kill-terminate-postgresql-hang-query/" title="How to kill / terminate PostgreSQL hang query">How to kill / terminate PostgreSQL hang query</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mkyong.com/database/how-to-get-random-row-from-sql-query-postgresql/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Convert Subquery Result to Array</title>
		<link>http://www.mkyong.com/database/convert-subquery-result-to-array/</link>
		<comments>http://www.mkyong.com/database/convert-subquery-result-to-array/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 09:15:40 +0000</pubDate>
		<dc:creator>mkyong</dc:creator>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[subquery]]></category>

		<guid isPermaLink="false">http://www.mkyong.com/?p=165</guid>
		<description><![CDATA[I want my subquery results (multi rows) return as a singale row in my query. For example.

SELECT u.url, &#40;SELECT c.categoryid FROM category c 
WHERE c.categoryid = u.categoryid&#41; FROM url u

If url contains multiple categories, subquery will return multiple rows as following
P.S Subquery return multiple rows is not supported in most of the database but PostgreSQL

url [...]]]></description>
			<content:encoded><![CDATA[<p>I want my subquery results (multi rows) return as a singale row in my query. For example.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> u<span style="color: #66cc66;">.</span>url<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> c<span style="color: #66cc66;">.</span>categoryid <span style="color: #993333; font-weight: bold;">FROM</span> category c 
<span style="color: #993333; font-weight: bold;">WHERE</span> c<span style="color: #66cc66;">.</span>categoryid <span style="color: #66cc66;">=</span> u<span style="color: #66cc66;">.</span>categoryid<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> url u</pre></div></div>

<p>If url contains multiple categories, subquery will return multiple rows as following<br />
P.S Subquery return multiple rows is not supported in most of the database but PostgreSQL</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">url <span style="color: #66cc66;">|</span> categoryid
<span style="color: #808080; font-style: italic;">---------------------</span>
<span style="color: #cc66cc;">1</span>   <span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">1</span>
<span style="color: #cc66cc;">1</span>   <span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">2</span>
<span style="color: #cc66cc;">2</span>   <span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">1</span></pre></div></div>

<p>Result i wanted is as following</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">url <span style="color: #66cc66;">|</span> categoryid
<span style="color: #808080; font-style: italic;">---------------------</span>
<span style="color: #cc66cc;">1</span>   <span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span>
<span style="color: #cc66cc;">2</span>   <span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">1</span></pre></div></div>

<p>I&#8217;m not sure how easy it can achieve in others database like oracle or mysql, however PostgreSQL provided Array data type, using array it&#8217;s very easy to get what i want. I have to changed my query to</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> u<span style="color: #66cc66;">.</span>url<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> array<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> c<span style="color: #66cc66;">.</span>categoryid <span style="color: #993333; font-weight: bold;">FROM</span> category c 
<span style="color: #993333; font-weight: bold;">WHERE</span> c<span style="color: #66cc66;">.</span>categoryid <span style="color: #66cc66;">=</span> u<span style="color: #66cc66;">.</span>categoryid<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #993333; font-weight: bold;">AS</span> categoryid <span style="color: #993333; font-weight: bold;">FROM</span> url u</pre></div></div>

<p>Result ~</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">url <span style="color: #66cc66;">|</span> categoryid
<span style="color: #808080; font-style: italic;">---------------------</span>
<span style="color: #cc66cc;">1</span>   <span style="color: #66cc66;">|</span>  <span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">2</span>   <span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">1</span></pre></div></div>

<p>Done ~ PostgreSQL Array Rock ~</p>
<ul class="related_post"><li><a href="http://www.mkyong.com/database/regular-expression-in-postgresql/" title="Regular Expression in PostgreSQL">Regular Expression in PostgreSQL</a></li><li><a href="http://www.mkyong.com/java/how-do-connect-to-postgresql-with-jdbc-driver-java/" title="How to connect to PostgreSQL with JDBC driver &#8211; Java">How to connect to PostgreSQL with JDBC driver &#8211; Java</a></li><li><a href="http://www.mkyong.com/database/postgresql-point-in-time-recovery-incremental-backup/" title="PostgreSQL Point-in-time Recovery (Incremental Backup)">PostgreSQL Point-in-time Recovery (Incremental Backup)</a></li><li><a href="http://www.mkyong.com/database/how-to-export-table-data-to-file-csv-postgresql/" title="How to export table data to file / csv &#8211; PostgreSQL">How to export table data to file / csv &#8211; PostgreSQL</a></li><li><a href="http://www.mkyong.com/database/how-to-kill-terminate-postgresql-hang-query/" title="How to kill / terminate PostgreSQL hang query">How to kill / terminate PostgreSQL hang query</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mkyong.com/database/convert-subquery-result-to-array/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to building PostgreSQL libpq Programs</title>
		<link>http://www.mkyong.com/database/how-to-building-postgresql-libpq-programs/</link>
		<comments>http://www.mkyong.com/database/how-to-building-postgresql-libpq-programs/#comments</comments>
		<pubDate>Sat, 19 Jul 2008 09:57:04 +0000</pubDate>
		<dc:creator>mkyong</dc:creator>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[C / C++]]></category>
		<category><![CDATA[libpq]]></category>
		<category><![CDATA[libpq-fe.h]]></category>
		<category><![CDATA[postgresql]]></category>

		<guid isPermaLink="false">http://www.mkyong.com/?p=126</guid>
		<description><![CDATA[Create and compile a program with PostgreSQL libpq is not so straightforward. I created a sample program like &#8220;testlibpq.c&#8221; in PostgreSQL documentation to test it.
When i compile it, i hit following error
libpq-fe.h: No such file or directory
PGconn’ undeclared (first use in this function)

1
2
3
4
5
6
7
8
9
10
11
12
13
mkyong@mkyong-desktop:~/Desktop/index$ gcc -o test.o test.c
test.c:8:22: error: libpq-fe.h: No such file or directory
test.c:11: error: [...]]]></description>
			<content:encoded><![CDATA[<p>Create and compile a program with PostgreSQL libpq is not so straightforward. I created a sample program like &#8220;testlibpq.c&#8221; in PostgreSQL documentation to test it.</p>
<p>When i compile it, i hit following error</p>
<p><b>libpq-fe.h: No such file or directory</b><br />
<b>PGconn’ undeclared (first use in this function)</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="linux" style="font-family:monospace;">mkyong@mkyong-desktop:~/Desktop/index$ gcc -o test.o test.c
test.c:8:22: error: libpq-fe.h: No such file or directory
test.c:11: error: expected ‘)’ before ‘*’ token
test.c: In function ‘main’:
test.c:21: error: ‘PGconn’ undeclared (first use in this function)
test.c:21: error: (Each undeclared identifier is reported only once
test.c:21: error: for each function it appears in.)
test.c:21: error: ‘conn’ undeclared (first use in this function)
test.c:22: error: ‘PGresult’ undeclared (first use in this function)
test.c:22: error: ‘res’ undeclared (first use in this function)
test.c:41: error: ‘CONNECTION_OK’ undeclared (first use in this function)
test.c:57: error: ‘PGRES_COMMAND_OK’ undeclared (first use in this function)
test.c:83: error: ‘PGRES_TUPLES_OK’ undeclared (first use in this function)</pre></td></tr></table></div>

<p>It do need to include <b> -I/usr/include/postgresql/ </b>  in order to compile it, please check your database or Linux administrator, or just type &#8220;pg_config &#8211;includedir&#8221; to find out where is PostgreSQL include file located. </p>
<p>However I still hit error like follwing</p>
<p><b>undefined reference to `PQfinish&#8217;</b><br />
<b> undefined reference to `PQconnectdb&#8217;</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="linux" style="font-family:monospace;">mkyong@mkyong-desktop:~/Desktop/index$ gcc -I/usr/include/postgresql/  -o test.o test.c
/tmp/ccoOJzAT.o: In function `exit_nicely':
test.c:(.text+0xd): undefined reference to `PQfinish'
/tmp/ccoOJzAT.o: In function `main':
test.c:(.text+0x57): undefined reference to `PQconnectdb'
test.c:(.text+0x65): undefined reference to `PQstatus'
test.c:(.text+0x74): undefined reference to `PQerrorMessage'
test.c:(.text+0xac): undefined reference to `PQexec'
test.c:(.text+0xba): undefined reference to `PQresultStatus'
test.c:(.text+0xca): undefined reference to `PQerrorMessage'
test.c:(.text+0xef): undefined reference to `PQclear'
test.c:(.text+0x105): undefined reference to `PQclear'
test.c:(.text+0x118): undefined reference to `PQexec'</pre></td></tr></table></div>

<p>Library is missing. Include PostgreSQL library pathwith  <b>-L/usr/lib/postgresql/8.3/lib/</b>, this all PostgreSQL  configuration information can get it from &#8220;pg_config&#8221;.</p>
<p>But I still hit the same error again</p>
<p><b>undefined reference to `PQfinish&#8217;</b><br />
<b> undefined reference to `PQconnectdb&#8221;</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="linux" style="font-family:monospace;">mkyong@mkyong-desktop:~/Desktop/index$ gcc -I/usr/include/postgresql/ -L/usr/lib/postgresql/8.3/lib/ -o test.o test.c
/tmp/ccgWnRJg.o: In function `exit_nicely':
test.c:(.text+0xd): undefined reference to `PQfinish'
/tmp/ccgWnRJg.o: In function `main':
test.c:(.text+0x57): undefined reference to `PQconnectdb'
test.c:(.text+0x65): undefined reference to `PQstatus'
test.c:(.text+0x74): undefined reference to `PQerrorMessage'
test.c:(.text+0xac): undefined reference to `PQexec'
test.c:(.text+0xba): undefined reference to `PQresultStatus'
test.c:(.text+0xca): undefined reference to `PQerrorMessage'
test.c:(.text+0xef): undefined reference to `PQclear'
test.c:(.text+0x105): undefined reference to `PQclear'
test.c:(.text+0x118): undefined reference to `PQexec'
test.c:(.text+0x126): undefined reference to `PQresultStatus'
test.c:(.text+0x136): undefined reference to `PQerrorMessage'
test.c:(.text+0x15b): undefined reference to `PQclear'
test.c:(.text+0x171): undefined reference to `PQclear'</pre></td></tr></table></div>

<p>It do need to specified exactly library with <b>-lpq</b> to include libpg library, correct command is like following</p>

<div class="wp_syntax"><div class="code"><pre class="linux" style="font-family:monospace;">gcc -I/usr/include/postgresql/ -L/usr/lib/postgresql/8.3/lib/ -lpq -o test.o test.c</pre></div></div>

<p>Done, compile without error.</p>
<ul class="related_post"><li><a href="http://www.mkyong.com/database/regular-expression-in-postgresql/" title="Regular Expression in PostgreSQL">Regular Expression in PostgreSQL</a></li><li><a href="http://www.mkyong.com/java/how-do-connect-to-postgresql-with-jdbc-driver-java/" title="How to connect to PostgreSQL with JDBC driver &#8211; Java">How to connect to PostgreSQL with JDBC driver &#8211; Java</a></li><li><a href="http://www.mkyong.com/database/postgresql-point-in-time-recovery-incremental-backup/" title="PostgreSQL Point-in-time Recovery (Incremental Backup)">PostgreSQL Point-in-time Recovery (Incremental Backup)</a></li><li><a href="http://www.mkyong.com/database/how-to-export-table-data-to-file-csv-postgresql/" title="How to export table data to file / csv &#8211; PostgreSQL">How to export table data to file / csv &#8211; PostgreSQL</a></li><li><a href="http://www.mkyong.com/database/how-to-kill-terminate-postgresql-hang-query/" title="How to kill / terminate PostgreSQL hang query">How to kill / terminate PostgreSQL hang query</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mkyong.com/database/how-to-building-postgresql-libpq-programs/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
