<?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>eXpand yOur cReativity &#187; postgres</title>
	<atom:link href="http://blog.bhushangahire.net/tag/postgres/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.bhushangahire.net</link>
	<description></description>
	<lastBuildDate>Thu, 05 Jan 2012 07:17:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>How Do I Enable remote access to PostgreSQL database server?</title>
		<link>http://blog.bhushangahire.net/2010/05/11/how-do-i-enable-remote-access-to-postgresql-database-server/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-do-i-enable-remote-access-to-postgresql-database-server</link>
		<comments>http://blog.bhushangahire.net/2010/05/11/how-do-i-enable-remote-access-to-postgresql-database-server/#comments</comments>
		<pubDate>Tue, 11 May 2010 12:11:22 +0000</pubDate>
		<dc:creator>Bhushan G Ahire</dc:creator>
				<category><![CDATA[Amazon EC2]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[remote]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://blog.bhushangahire.net/?p=177</guid>
		<description><![CDATA[By default, PostgreSQL database server remote access disabled for security reasons. However, some time you need to provide the remote access to database server from home computer or from web server. Step # 1: Login over ssh if server is outside your IDC Login over ssh to remote PostgreSQL database server: $ ssh user@remote.pgsql.server.com Step [...]]]></description>
			<content:encoded><![CDATA[<div class="format_text entry-content">
<div style="float: right; margin-top: 0px; margin-left: 5px;"><a title="See all Postgresql related tips/articles" href="http://www.cyberciti.biz/tips/category/postgresql/"><br />
<img src="http://figs.cyberciti.biz/3rdparty/pgsql-logo.png" border="0" alt="" /></a></div>
<p>By default, PostgreSQL database server remote access disabled for security reasons. However, some time you need to provide the remote access to database server from home computer or from web server.</p>
<h2>Step # 1: Login over ssh if server is outside your IDC</h2>
<p>Login over ssh to remote PostgreSQL  database server:</p>
<p><code>$ ssh user@remote.pgsql.server.com</code></p>
<h2>Step # 2: Enable client authentication</h2>
<p>Once connected, you need edit the PostgreSQL configuration file, edit the PostgreSQL configuration file <strong>/var/lib/pgsql/data/pg_hba.conf</strong> (or <strong>/etc/postgresql/8.2/main/pg_hba.conf</strong> for latest 8.2 version) using a text editor such as vi.</p>
<p>Login as postgres user using su / sudo command, enter:</p>
<p><code>$ su - postgres</code></p>
<p>Edit the file:</p>
<p><code>$ vi /var/lib/pgsql/data/pg_hba.conf</code></p>
<p>OR</p>
<p><code>$ vi /etc/postgresql/8.2/main/pg_hba.conf</code></p>
<p>Append the following configuration lines to give access to 10.10.29.0/24 network:</p>
<p><code>host all all 10.10.29.0/24 trust</code></p>
<p>Save and close the file.  Make sure you replace 10.10.29.0/24 with  actual network IP address range of the clients system in your own network.</p>
<h2>Step # 2: Enable networking for PostgreSQL</h2>
<p>You need to enable TCP / IP networking. Use either step #3 or #3a as per your PostgreSQL database server version.</p>
<h2>Step # 3: Allow TCP/IP socket</h2>
<p>If you are using <strong>PostgreSQL version 8.x or newer</strong> use the following instructions or skip to <a href="#3a">Step # 3a</a> for older version (7.x or older).</p>
<p>You need to open PostgreSQL configuration file /var/lib/pgsql/data/postgresql.conf or /etc/postgresql/8.2/main/postgresql.conf.</p>
<p><code># vi /etc/postgresql/8.2/main/postgresql.conf</code></p>
<p>OR</p>
<p><code># vi /var/lib/pgsql/data/postgresql.conf </code></p>
<p>Find configuration line that read as follows:</p>
<p><code>listen_addresses='localhost'</code></p>
<p>Next set IP address(es) to listen on; you can use comma-separated list of addresses; defaults to &#8216;localhost&#8217;, and &#8216;*&#8217; is all ip address:</p>
<p><code>listen_addresses='*'</code></p>
<p>Or just bind to 202.54.1.2 and 202.54.1.3 IP address</p>
<p><code>listen_addresses='202.54.1.2 202.54.1.3'</code></p>
<p>Save and close the file. Skip to <a href="#4">step # 4</a>.</p>
<h3>Step #3a &#8211; Information for old version 7.x  or older</h3>
<p>Following configuration only required for <strong>PostgreSQL version 7.x or older</strong>. Open config file, enter:</p>
<p><code># vi /var/lib/pgsql/data/postgresql.conf </code></p>
<p>Bind and open TCP/IP port by setting tcpip_socket to true.  Set / modify tcpip_socket to true:</p>
<p><code>tcpip_socket = true</code></p>
<p>Save and close the file.</p>
<h2>Step # 4: Restart PostgreSQL Server</h2>
<p>Type the following command:</p>
<p><code># /etc/init.d/postgresql restart</code></p>
<h2>Step # 5: Iptables firewall rules</h2>
<p>Make sure iptables is not blocking communication, <a href="http://www.cyberciti.biz/tips/howto-iptables-postgresql-open-port.html">open port 5432</a> (append rules to your iptables scripts or file <a href="http://www.cyberciti.biz/faq/howto-block-ipaddress-of-spammers-with-firewall/">/etc/sysconfig/iptables</a>):</p>
<pre>iptables -A INPUT -p tcp -s 0/0 --sport 1024:65535 -d 20.04.23.22  --dport 5432 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp -s 20.04.23.22 --sport 5432 -d 0/0 --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT</pre>
<p>Restart firewall:</p>
<p><code># /etc/init.d/iptables restart</code></p>
<h2>Step # 6: Test your setup</h2>
<p>Use psql command from client system. Connect to remote server using IP address 20.04.23.22 and login using postgres username and testdatabase database, enter:</p>
<p><code>$ psql -h 20.04.23.22 -U postgres -d testdatabase</code></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.bhushangahire.net/2010/05/11/how-do-i-enable-remote-access-to-postgresql-database-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing PostgreSQL on Snow Leopard 10.6</title>
		<link>http://blog.bhushangahire.net/2010/02/15/installing-postgresql-on-snow-leopard-10-6/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=installing-postgresql-on-snow-leopard-10-6</link>
		<comments>http://blog.bhushangahire.net/2010/02/15/installing-postgresql-on-snow-leopard-10-6/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 08:41:59 +0000</pubDate>
		<dc:creator>Bhushan G Ahire</dc:creator>
				<category><![CDATA[MAC Tiger]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[port]]></category>
		<category><![CDATA[postgres]]></category>

		<guid isPermaLink="false">http://blog.bhushangahire.net/?p=152</guid>
		<description><![CDATA[Installing PostgreSQL 8.3 First, you’ll need to install Xcode if you haven’t already. This is available on the Snow Leopard DVD in the Optional Installs directory. Second, if you aren’t already using it, download Mac Ports for Snow Leopard and install it. Mac Ports has come a long way in the last few years and [...]]]></description>
			<content:encoded><![CDATA[<div class="entry">
<h2>Installing PostgreSQL 8.3</h2>
<p>First, you’ll need to install Xcode if you haven’t already. This is available on the Snow Leopard DVD in the Optional Installs directory.</p>
<p>Second, if you aren’t already using it, download <a href="http://distfiles.macports.org/MacPorts/MacPorts-1.8.0-10.6-SnowLeopard.dmg">Mac Ports for Snow Leopard</a> and install it. Mac Ports has come a long way in the last few years and will make your life much easier.</p>
<p>Once those are installed, run the following command:<br />
<code>sudo port install postgresql83 postgresql83-server</code></p>
<h2>Setup Your First Database</h2>
<p>At the very end of the install it tells you how to setup your first database:</p>
<p><code>sudo mkdir -p /opt/local/var/db/postgresql83/defaultdb</code><br />
<code>sudo chown postgres:postgres /opt/local/var/db/postgresql83/defaultdb</code><br />
<code>sudo su postgres -c '/opt/local/lib/postgresql83/bin/initdb -D /opt/local/var/db/postgresql83/defaultdb'</code></p>
<p>You’ll also want to setup Postgres to auto-run as a server on start up.</p>
<p><code>sudo launchctl load -w /Library/LaunchDaemons/org.macports.postgresql83-server.plist</code></p>
<p>If you want to start it right now, you can either reboot or do the following:</p>
<p><code>sudo su postgres -c '/opt/local/lib/postgresql83/bin/postgres -D /opt/local/var/db/postgresql83/defaultdb'</code></p>
<h2>Make psql Available from the Command Line</h2>
<p>The executable files for PostgreSQL get shoved into a non-standard place (just like MySQL), so you’ll need to edit the default profile.</p>
<p><code>sudo vi /etc/profile</code></p>
<p>You can also do this using <code>sudo mate /etc/profile</code> if you aren’t comfortable in VI.</p>
<p>The <code>PATH=</code> line needs to be changed to include the PostgreSQL bin directory.</p>
<p>Mine was <code>PATH="/opt/local/bin:$PATH"</code> and is now:</p>
<p><code>PATH="/opt/local/bin:/opt/local/sbin:/opt/local/lib/postgresql83/bin:$PATH"</code></p>
<p>If you open a new terminal window you can now type <code>psql</code> and it will find it.</p>
<h2>Create a New User and Database</h2>
<p>By default, PostgreSQL creates a <em>postgres</em> user for you. However, it’s not good practice to use the default and it’s a pain in the ass. Let’s just create a new database user to make it easier.</p>
<p><code>createuser --superuser macusername -U postgres</code></p>
<p>You need to change <code>macusername</code> to your mac username. This will make your life ALOT easier. Trust me here.</p>
<p>Next, create your database:</p>
<p><code>createdb my_database</code></p>
<h2>Installing the PostgreSQL Ruby Gem</h2>
<p>Unlike the MySQL driver, we don’t need to pass the ARCHFLAGS variable as 64 bit. PostgreSQL comes with both 32 and 64-bit versions. Yeah!</p>
<p><code>sudo gem install postgres-pr</code></p>
<p>Per Tom’s comment below, we should be using the native driver for better performance.</p>
<p><code>sudo env ARCHFLAGS="-arch x86_64" gem install pg</code></p>
<h2>Configuring your Rails Application</h2>
<p>Inside your Ruby on Rails application, open up config/database.yml and change your development adapter to be similar to the following:</p>
<p><code>development:<br />
  adapter: postgresql<br />
  database: defaultdb<br />
  username: defaultdb<br />
</code></p>
<p>You can change <code>defaultdb</code> to the name you need for your application.</p>
</p></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.bhushangahire.net/2010/02/15/installing-postgresql-on-snow-leopard-10-6/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Grant privileges to all tables in a database for postgresql</title>
		<link>http://blog.bhushangahire.net/2009/06/17/grant-privileges-to-all-tables-in-a-database-for-postgresql/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=grant-privileges-to-all-tables-in-a-database-for-postgresql</link>
		<comments>http://blog.bhushangahire.net/2009/06/17/grant-privileges-to-all-tables-in-a-database-for-postgresql/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 11:50:08 +0000</pubDate>
		<dc:creator>Bhushan Ahire</dc:creator>
				<category><![CDATA[Security]]></category>
		<category><![CDATA[grant]]></category>
		<category><![CDATA[permissions]]></category>
		<category><![CDATA[postgres]]></category>

		<guid isPermaLink="false">http://blog.bhushangahire.net/?p=127</guid>
		<description><![CDATA[Grant privileges to all tables in a database (select, update, insert, delete) Eg:( Creating a read-only user in postgres) –Function to grant access(select,insert,update,delete) to users CREATE FUNCTION pg_grant(TEXT, TEXT, TEXT, TEXT) RETURNS integer AS ' DECLARE obj record; num integer; BEGIN num:=0; FOR obj IN SELECT relname FROM pg_class c JOIN pg_namespace ns ON (c.relnamespace [...]]]></description>
			<content:encoded><![CDATA[<div class="snap_preview">
<p>Grant privileges to all tables in a database (select, update, insert, delete)</p>
<p>Eg:( Creating a read-only user in postgres)</p>
<p>–Function to grant access(select,insert,update,delete) to users<br />
<code><br />
CREATE FUNCTION pg_grant(TEXT, TEXT, TEXT, TEXT)<br />
RETURNS integer AS '<br />
DECLARE obj record;<br />
num integer;<br />
BEGIN<br />
num:=0;<br />
FOR obj IN SELECT relname FROM pg_class c<br />
JOIN pg_namespace ns ON (c.relnamespace = ns.oid) WHERE<br />
relkind in (''r'',''v'',''S'') AND<br />
nspname = $4 AND<br />
relname LIKE $3<br />
LOOP<br />
EXECUTE ''GRANT '' || $2 || '' ON '' || obj.relname || '' TO '' || $1;<br />
num := num + 1;<br />
END LOOP;<br />
RETURN num;<br />
END;<br />
' LANGUAGE plpgsql SECURITY DEFINER;<br />
</code><br />
–Function to revoke access(select,insert,update,delete) from users<br />
<code><br />
CREATE FUNCTION pg_revoke(TEXT, TEXT, TEXT, TEXT)<br />
RETURNS integer AS '<br />
DECLARE obj record;<br />
num integer;<br />
BEGIN<br />
num:=0;<br />
FOR obj IN SELECT relname FROM pg_class c<br />
JOIN pg_namespace ns ON (c.relnamespace = ns.oid) WHERE<br />
relkind in (''r'',''v'',''S'') AND<br />
nspname = $4 AND<br />
relname LIKE $3<br />
LOOP<br />
EXECUTE ''REVOKE '' || $2 || '' ON '' || obj.relname || '' FROM '' || $1;<br />
num := num + 1;<br />
END LOOP;<br />
RETURN num;<br />
END;<br />
' LANGUAGE plpgsql SECURITY DEFINER;<br />
</code><br />
–Create users for your database<br />
<code><br />
CREATE USER userreadonly WITH PASSWORD 'userr3ad0nly';<br />
CREATE USER userall WITH PASSWORD 'usersh0pa11';<br />
</code><br />
–Grant respective access to users<br />
<code><br />
select pg_grant('</code><code>userreadonly </code><code>','select','%','public');<br />
select pg_grant('</code><code>userall </code><code>','select,insert,update,delete','%','public');<br />
</code></p>
<p>You might need to create lang for plpgsql if you had not done so<br />
<code><br />
createlang plpgsql yrdatabasename</code></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.bhushangahire.net/2009/06/17/grant-privileges-to-all-tables-in-a-database-for-postgresql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

