<?xml version="1.0"?>
<rss version="2.0">
<channel>
  <title>Weiqi Gao&#039;s Observations - postgresql tag</title>
  <link>http://www.weiqigao.com/blog/tags/postgresql/</link>
  <description>Sharing My Experience...</description>
  <language>en</language>
  <copyright>Weiqi Gao</copyright>
  <lastBuildDate>Fri, 11 May 2012 12:48:36 GMT</lastBuildDate>
  <generator>Pebble (http://pebble.sourceforge.net)</generator>
  <docs>http://backend.userland.com/rss</docs>
  
  <image>
    <url>http://pebble.sourceforge.net/common/images/powered-by-pebble.gif</url>
    <title>Weiqi Gao&#039;s Observations</title>
    <link>http://www.weiqigao.com/blog/</link>
  </image>
  
  
  <item>
    <title>PostgreSQL 8.1.0 Comes To Cygwin</title>
    <link>http://www.weiqigao.com/blog/2005/11/17/postgresql_8_1_0_comes_to_cygwin.html</link>
    
      
        <description>
          &lt;p&gt;For the longest time, my development relational database of choice has been &lt;a href= &#034;http://www.postgresql.org/&#034; &gt;PostgreSQL&lt;/a&gt;.  It offers the features that I care about (the simple stuff: create table, select from where order by group by, transactions, JDBC drivers) on the platforms that I use the most often (GNU/Linux, Windows/Cygwin) under a BSD license.&lt;/p&gt;

&lt;p&gt;What I like the most is the fact that it comes with the &lt;a href= &#034;http://redhat.com/&#034; &gt;Red Hat&lt;/a&gt; distribution since the early days.  For the last few years, a Cygwin compiled version also showed up as part of the &lt;a href= &#034;http://cygwin.com/&#034; &gt;Cygwin net distribution&lt;/a&gt;.&lt;p&gt;

&lt;p&gt;The Cygwin version, however, was broken earlier this summer.  And I was without a database on Windows for a while.  Finally last week, &lt;a href= &#034;http://xarch.tu-graz.ac.at/~rurban/&#034; &gt;Reini Urban&lt;/a&gt; announced the &lt;a href= &#034;http://sources.redhat.com/ml/cygwin-announce/2005-11/msg00006.html&#034; &gt;availability of PostgreSQL 8.0.4 and 8.1.0&lt;/a&gt; for Cygwin.&lt;/p&gt;

&lt;p&gt;Setting up PostgreSQL as a Windows service has always been a tricky endeavor.  The new release offers some relief by providing an init script that can simplify the process.  According to the &lt;a href= &#034;http://xarch.tu-graz.ac.at/home/rurban/software/cygwin/release/postgresql/README&#034; &gt;release notes&lt;/a&gt;, it could be as simple as:&lt;/p&gt;

&lt;pre style=&#034;margin-left:3em&#034;&gt;/etc/rc.d/init.d/postgresql initdb
/etc/rc.d/init.d/postgresql install
/etc/rc.d/init.d/postgresql start&lt;/pre&gt;

&lt;p&gt;While this is essentially true, there are some hurdles that I have to jump over to have everything up and running.  Let me just say that the script can be improved upon.  Here&#039;s my Cygwin PostgreSQL 8.1.0 setup first impression:&lt;/p&gt;

&lt;ul&gt;

&lt;li&gt;First of all, the &lt;tt&gt;CYGWIN&lt;/tt&gt; environment variable needs to contain the word &lt;tt&gt;server&lt;/tt&gt;.  (I have &lt;tt&gt;CYGWIN=ntsec server&lt;/tt&gt;.)&lt;/li&gt;

&lt;li&gt;The &lt;tt&gt;cygserver&lt;/tt&gt; service needs to be installed already.  (Running &lt;tt&gt;cygserver-config&lt;/tt&gt; once will do it.)&lt;/li&gt;

&lt;li&gt;The &lt;tt&gt;initdb&lt;/tt&gt; task will create the database template in &lt;tt&gt;/usr/share/postgresql/data&lt;/tt&gt; and create a directory &lt;tt&gt;/tmp/postgresql&lt;/tt&gt; for runtime use.&lt;/li&gt;

&lt;li&gt;After a fresh installation, the directory &lt;tt&gt;/usr/share/postgresql&lt;/tt&gt; already contains some files and subdirectories.  &lt;tt&gt;initdb&lt;/tt&gt; won&#039;t like it if &lt;tt&gt;/usr/share/postgresql/data&lt;/tt&gt; already exists and is non-empty or if &lt;tt&gt;/tmp/postgresql&lt;/tt&gt; exists.  (A failed run of &lt;tt&gt;initdb&lt;/tt&gt; will create such a situation.)&lt;/li&gt;

&lt;li&gt;There are plenty of file/directory permission problems that would cause the &lt;tt&gt;install&lt;/tt&gt; or the &lt;tt&gt;start&lt;/tt&gt; steps to fail.  All because the service is run as &lt;tt&gt;Local System&lt;/tt&gt;, not the logged in user.  Files and directories that need to open up permission include

&lt;ul&gt;
&lt;li&gt;&lt;tt&gt;/usr/sbin/postgres.exe&lt;/tt&gt; (needs &lt;tt&gt;chmod ugo+rx&lt;/tt&gt;),&lt;/li&gt;

&lt;li&gt;&lt;tt&gt;/usr/share/postgresql/data&lt;/tt&gt; (needs &lt;tt&gt;chmod ugo+rx&lt;/tt&gt;),&lt;/li&gt;

&lt;li&gt;&lt;tt&gt;/tmp/postgresql&lt;/tt&gt; (needs &lt;tt&gt;chmod ugo+rwx&lt;/tt&gt;), &lt;/li&gt;

&lt;li&gt;&lt;tt&gt;/usr/share/postgresql/data/global&lt;/tt&gt; (needs &lt;tt&gt;chmod ug+rwx&lt;/tt&gt;.)&lt;/li&gt;
&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;The &lt;tt&gt;/etc/rc.d/init.d/postgresql&lt;/tt&gt; script contains a chunk of code to ensure that the &lt;tt&gt;CYGWIN&lt;/tt&gt; environment variable contains the word &lt;tt&gt;server&lt;/tt&gt;.  But it went too far.  This caused the &lt;tt&gt;install&lt;/tt&gt; task to fail with an error message saying &#034;CYGWIN must contain server&#034; even though my &lt;tt&gt;CYGWIN&lt;/tt&gt; already does.&lt;/li&gt;

&lt;li&gt;The log file at &lt;tt&gt;/var/log/postgresql.log&lt;/tt&gt; provided valuable information while I&#039;m trouble shooting my setup.&lt;/li&gt;

&lt;li&gt;The &lt;tt&gt;psql&lt;/tt&gt; (PostgreSQL&#039;s interactive SQL command line tool) seems to look for a UNIX domain socket to connect to at the wrong place (&lt;tt&gt;/tmp/&lt;span style=&#034;color:magenta&#034;&gt;.s.PGSQL.5432&lt;/span&gt;&lt;/tt&gt; instead of &lt;tt&gt;/tmp/postgresql/&lt;span style=&#034;color:magenta&#034;&gt;.s.PGSQL.5432&lt;/span&gt;&lt;/tt&gt;.)  I have to connect to the TCP socket by running &lt;tt&gt;psql -h localhost&lt;/tt&gt;.&lt;/li&gt;

&lt;li&gt;The bootstrap user Id for PostgreSQL is &lt;tt&gt;SYSTEM&lt;/tt&gt;.  So to add my own login to PostgreSQL I need to run &lt;tt&gt;/usr/sbin/createuser -U SYSTEM&lt;/tt&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Here&#039;s the portion of &lt;tt&gt;/etc/rc.d/init.d/postgresql&lt;/tt&gt; I modified&lt;/p&gt;

&lt;pre&gt;    # check for CYGWIN containing server
    if ! echo $CYGWIN | grep -q server
    then
        echo &#034;ERROR CYGWIN must contain server for cygserver to work properly&#034;
        exit 1
    fi&lt;/pre&gt;

&lt;p&gt;The &lt;tt&gt;if&lt;/tt&gt; line used to say&lt;/p&gt;
&lt;pre&gt;    if [ -n &#034;${CYGWIN%server}&#034; ]&lt;/pre&gt;

&lt;p&gt;It sounds like a lot written down, but the actual trial and error of the setup is pretty easy and It took me less then 30 minutes to figure all of these out.  What is important is that at the end, I have a functional PostgreSQL server running as a Windows service again:&lt;/p&gt;

&lt;pre style=&#034;margin-left:3em&#034;&gt;[weiqi@gao] $ &lt;b&gt;psql -h localhost&lt;/b&gt;
Welcome to psql 8.1.0, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

weiqi=# &lt;b&gt;create table people (first_name varchar, last_name varchar);&lt;/b&gt;
CREATE TABLE
weiqi=# &lt;b&gt;insert into people values (&#039;Weiqi&#039;, &#039;Gao&#039;);&lt;/b&gt;
INSERT 0 1
weiqi=# &lt;b&gt;select * from people;&lt;/b&gt;
 first_name | last_name
------------+-----------
 Weiqi      | Gao
(1 row)

weiqi=# &lt;b&gt;\q&lt;/b&gt;
[weiqi@gao] $
&lt;/pre&gt;
        </description>
      
      
    
    
    
    <comments>http://www.weiqigao.com/blog/2005/11/17/postgresql_8_1_0_comes_to_cygwin.html#comments</comments>
    <guid isPermaLink="true">http://www.weiqigao.com/blog/2005/11/17/postgresql_8_1_0_comes_to_cygwin.html</guid>
    <pubDate>Fri, 18 Nov 2005 05:06:57 GMT</pubDate>
  </item>
  
  </channel>
</rss>

