« February 2008 | Main | April 2008 »

March 2008 Archives

March 11, 2008

Configuring PostgreSQL 8.2 in OS X Leopard via Fink

I spent a bit of time trying to figure this out, and instead of having to relearn this painful lesson in the future, I thought I would preserve my tribulations for prosperity. There were a number of resources that provide partial guidance in discovering the solution (which I've included here), but here is the solution laid out in full:

1. Install Fink

If you haven't already, install Fink. As per the Fink install instructions, you will also need the Apple Developer Tools. The package tools which come with Fink are second to none. To pull down the latest version of a package, simply install it using FinkCommander from source or binaries.

2. Install PostgreSQL

At the time of writing this, the latest version of PostgreSQL is 8.2.6. It is highly recommended that you use the latest available stable release of PostgreSQL as it usually includes a number of security and stability fixes which are a prerequisite for maintaining a stable, secure installation. To install PostgreSQL from the command line, you could also run the following command:

$ /sw/bin/fink install postgres

Simply follow the prompts to complete the installation.

3. Create PostgreSQL account

I found that previous versions of PostgreSQL left some stranded permissions on my system. Unfortunately, none of these mechanisms allowed me to sudo into the postgres account. Given the change in user management tools in Leopard, it is recommended that you use the new Directory Service commands provided in 10.5.x to add the PostgreSQL user to the system. If you have a previous version of PostgreSQL installed that you are not currently using, I recommend removing it and any associated users from the system. Exercise caution doing this if you have an existing installation as it has the potential to loose your data if you are not careful. As the PostgreSQL account used to manage the database should behave like other system accounts, you will need to associate with an unused user and group ID which is less than 500. This will prevent it from being displayed in the Login Screen. I modified Pat Maddox's instructions to determine which whether or not a particular ID is currently in use by issuing the following commands:

$ dscl . -list /Groups PrimaryGroupID | awk '{print $2}' | grep user_id
$ dscl . -list /Users UniqueID | awk '{print $2}' | grep group_id

where user_id and group_id are the IDs you would like to check. If the command returns a value, then the ID is already in use. In most cases, user/group ID 499 is available for use. The rest of this example uses this for the PostgreSQL user/group ID. To create the PostgreSQL group account issue the following command:

$ sudo dseditgroup -o create -i 499 -r "Postgres Admin User" pgsql

To create the PostgreSQL user account issue the following commands:

$ sudo dscl .
> create /Users/pgsql
> create /Users/pgsql UniqueID 102
> create /Users/pgsql UserShell /bin/bash
> create /Users/pgsql RealName "Postgres Administrator"
> create /Users/pgsql NFSHomeDirectory /usr/local/pgsql
> create /Users/pgsql PrimaryGroupID 102
> create /Users/pgsql Password *
> quit

4. Configure directories

This is where Maddox's instructions (based on MacPorts) and mine diverge. Fink typically installs packages under the /sw directory. The main PostgreSQL binaries should be located in the bin subdirectory with the database files installed in the var subdirectory. The database directory usually has a version suffix. Once you've located the database directory, you will need to reassign it's ownership to the newly created pgsql user. I usually provide a symbolic link which points to the most recent version installed on the system to provide a short-cut as well as indicate which version is being used (when more than one version is installed).

$ sudo chown -R pgsql:pgsql /sw/var/postgresql-8.2
$ cd /sw/var
$ sudo ln -s postgresql-8.2 postgresql

You will notice that PostgreSQL was not installed in /usr/local by Fink. This avoids potential conflicts which might arise with other 3rd party packages installed on the system. You will also notice that when we created the PostgreSQL account, the NFSHomeDirectory attribute was set to /usr/local/pgsql. This was intentional. By creating a symbolic link from this directory to the one created by Fink, we can integrate the Fink installation seemlessly in with the rest of the system without introducing conflicts or dependencies. I usually prefer relative paths in this circumstance:

$ cd /usr/local
$ sudo ln -s ../../sw/var/postgresql ./pgsql

By pointing to the original symbolic link, simply changing that link will automatically update the NFSHomeDirectory attribute associated with the PostgreSQL user.

5. Access caveats

Resist the urge to associate a password with the pgsql user. Using sudo to assume privileges will allow you to limit who can access your PostgreSQL installation locally. If you require remote administrative privileges, configure accounts through PostgreSQL directly instead of delegating privileges to system accounts.

6. Configure database

At this point, you should be able to switch to the pgsql user. Check your identity and privileges:

$ sudo su - pgsql
$ whoami
$ id pgsql

To initialize the database issue the following commands:

$ /sw/bin/initdb -D /usr/local/pgsql/data -E utf8

This will create the initial postgres database which will support UTF8 (for international character sets). Once the database is initialized, you can start it by issuing the following command:

$ /sw/bin/pg_ctl -D /usr/local/pgsql/data -l /usr/local/pgsql/logfile start

To stop the server:

$ /sw/bin/pg_ctl -D /usr/local/pgsql/data stop

To enable the procedural language for all databases derived from template1:

$ /sw/bin/createlang-8.2 plpgsql template1

You should now be able to connect to the postgres database:

$ psql -d postgres

7. Security

At this point you might want to spend some time securing your PostgreSQL installation. There are a few pointers in the Installing PostgreSQL on Mac OS X article, but as with all things Postgres, please refer to the Manual for definitive answers regarding security. One of the few things I did to secure my installation was to modify the data/pg_hba.conf file to support a default administrative user that required password authentication. This prevents a local user from automatically gaining access to the database by compromising the PostgreSQL user account. This required first assigning a password to the PostgreSQL user account. This is accomplished by connecting to the newly created postgres database and executing a SQL statement which assigns the user a password:

$ psql -d postgres
#postgres=# ALTER USER pgsql PASSWORD 'secret';
#postgres=# \q

where secret is the password that you've selected. The password itself will be stored as an MD5 checksum so that when you authenticate against the PostgreSQL server, it is never transmitted in plain text. Next, the data/pg_hba.conf file needs to be modified to support authentication for the PostgreSQL user account. Use an editor to add the following lines:

local    all         pgsql                             md5
# IPv4 local connections:
host    all         pgsql     127.0.0.1/32             md5
# IPv6 local connections:
host    all         pgsql        ::1/128               md5

After these changes, a server restart will be required:

$ pgctl -D /sw/var/postgres/data reload

When logging into the default database you should now be prompted for a password. Be careful if lines such as the above already exist in your configuration. You may need to replace them in order for this to work.

8. Tuning

As this database is going to be used for development purposes, a lot of cruft will accumulate in the database as tables are create and destroyed and data is modified. To save the trouble of having to automatically clean up the database, you can modify the data/postgresql.conf file to support auto-vacuuming to recover space in the background. The following lines should be placed in the file:

stats_start_collector = on
stats_row_level = on
autovacuum = on 

As the postgresql.conf file is installed with defaults on a fresh PostgreSQL install, you may need to simply comment out these lines.

9. System service configuration

The last step requires configuring the PostgreSQL install to start and stop as other OS X system services do. This will require creating a StartupItem entry for PostgreSQL. These basis of these instructions are courtesy of Robert Crews and wmaus. First create a directory to store the initialization script for PostgreSQL:

$ sudo mkdir /Library/StartupItems/PostgreSQL

Create the PostgreSQL script under this directory with the following code:

#!/bin/sh

# Source common setup, including /etc/hostconfig
. /etc/rc.common

FINK_BIN='/sw/bin';
PGSQL_HOME='/sw/var/postgresql';
PGSQL_USER='pgsql';

StartService ( ) {
    # Don't start unless PostgreSQL is enabled in /etc/hostconfig
    if [ "${POSTGRESQL:-NO-}" = "-YES-" ]; then
        ConsoleMessage "Starting PostgreSQL"
        sudo -u ${PGSQL_USER} ${FINK_BIN}/pg_ctl \
            -D ${PGSQL_HOME}/data \
            -l ${PGSQL_HOME}/logfile start
    fi
}

StopService ( ) {
    ConsoleMessage "Stopping PostgreSQL"
    sudo -u ${PGSQL_USER} ${FINK_BIN}/pg_ctl -D ${PGSQL_HOME}/data stop
}

RestartService ( ) {
    if [ "${POSTGRESQL:-NO-}" = "-YES-" ]; then
        ConsoleMessage "Restarting PostgreSQL"
        StopService
        StartService
    else
        StopService
    fi
}

RunService "$1"

Obviously, replace the constants at the beginning of the script to match your installation if they are different from the ones here. There is one step that is not described in the Crew's instructions which is required to allow the SystemStarter to manage the service. A PostgreSQL.loc file in the same directory as the script which contains the path to the postgres binary and provide the execution context for pg_ctl must be created. In the case of a Fink installed PostgreSQL installation, this file should contain the following line:

/sw/bin

Without this .loc file, the SystemStarter will throw an error when invoked.

The last step requires adding a PostgreSQL entry in the /etc/hostconfig file. It's always a good idea to save a copy of the original config file before modifying it. Add the following entry to the file:

POSTGRESQL=-YES-

This will ensure that PostgreSQL is started when you boot your machine. You can also manage the service using the following commands:

$ sudo SystemStarter stop PostgreSQL
$ sudo SystemStarter start PostgreSQL
$ sudo SystemStarter restart PostgreSQL

Drop me a line if you find this useful or if you find any mistakes in the method outlined above.

March 14, 2008

Lost Episode 5 Redux - The Constant

When you watch television, do you ever wonder why you should be watching a show in the first place? Not the passive consumption that constitutes the majority of television viewing experience, but the active engagement good programmes illicit from their viewers?

I've been a long time fan of LOST, and the episode, "The Constant" reaffirms my belief that the not only is this series an exemplar of modern epic story-telling, but that it approaches what can only be defined as Art. At the heart of all compelling modern fabrications is an archetype formed from our "collective unconsciousness", as Jung put it. All of the main characters in LOST exhibit this characteristic. Kate, embodies the proverbial "Girl-Next-Door", Sawyer embodies the "Bad-Boy", while Locke is "Prophet" archetype.

Desmond, the character whose story is revealed in this episode embodies one of our most important collective archetypes; the "Hero". We've corrupted the idea of "Hero" with muscle bound stereotypes devoid of the essential character of what "Hero" means. The physicality of the "Hero" is obviously important, for it allows our idea of "Hero" to surmount worldly obstacles, but the obstacles themselves are existential in nature. We associated the superhuman to the "Hero", but by embuing the archetype with a power rooted in the human experience as Desmond is, allows him to use it selflessly instead for self-aggrandizement.

The fact that Desmond's power is over time itself gives context and scale to the depth of his achievement.

For the "Hero" archetype to truly achieve universality, the "Hero" must also embody the idea of sacrifice. In Desmond's case, his sacrifice was Penny.

The Desmond in the past that was aware of the Future and the 2004 Desmond that was aware of only 1996 understood the circumstances of his displaced selves and accepted the unfolding years without hesitation. The realization that the apparent contradiction could only be resolved through 8 years of journey only made his sacrifice even more heroic in proportions.

Heroism is confronting an unequivocal outcome and having the courage to accept it. Desmond's story resonates for this reason; at the heart of all heroic acts is the promise of Hope. That the creator's of LOST managed to capture this essence of this conflict and express it in a 38 minute television episode is sheer artistry.

March 18, 2008

Catharsis and RPG

I must admit, that sometime ago, I was addicted to Diablo 3. Something about the game occupied my every waking moment and for two weeks solid, I was glued to my computer. No ergonomic mouse was ever designed for this type of prolonged use and by the end of it, my hand was fixed in a rictus of mouse clicking. (You know you've been over doing it when you develop calluses where your palm rests on the mouse.)

After this extended bout of insanity, I had an epiphany that has dulled my enthusiasm for such fare - I had been a dupe at the end of an elaborate Skinner black box. Nothing cramps desire like coming to the realization that you've been manipulated into giving in to your inner monkey. The obsession with the game fed off the primal need for a reward completely disproportionate to the effort expended, but somehow, I had managed to rationalize the effort as "fun". What was I thinking???

There is something else that I should admit which is related to my fascination with video games; that I am a long time RPGer of the Dungeons & Dragons variety (the pen and paper, polyhedra wielding, geohex terrain loving, leaded miniture painting kind). I suspect that few people could claim that their RPG group has lasted some 25 years other than those involved in the industry itself. We've played (and continue to play) every game under the sun from Tunnels and Trolls and Paranoia, to our (relatively recent) work-horses of Earthdawn, ShadowRun, and TORG.

I always associated my love of video games and role playing as coming from the same source - my fascination with the fantastic (as evident from the SciFi/Fantasy ladened literature of my youth) and the acknowledgment that the collective engagement of our imaginations produced some of our most memorable moments of "the Game". The collective conscious fed by books, arcades, movies, and comics, contributed in no small part to the absurd hilarity and outright originality that permeated our gaming sessions.

It's taken me a while to be able to articulate my misgivings regarding the direction of computerized RPGs, though it's been steadily building. Not that I'm completely oppose computerized games, but there are a variety that certainly deserves the "heroine-ware" appellation.

My problem with these games is what lies at the heart of the game - why do gamers play games in the first place? I think the answer to this question highlights a fundamental difference between two genres of gaming which are superficially similar, but are governed by different motivations.

Part of appeal of RPG is the creative act of imagination that computerized RPGs literalize to the detriment of the whole experience. To overcome this limitation, game designers replace this void with artificial reward systems to entice players into continuing to play. The problem with these environments is that by introducing the phantom reward of level advancement, players never achieve the true reward of role-playing - catharsis. In a sense, delaying catharsis or completely eliminating it allows for players to become entrapped in the button clicking unreality where reward is dangled tantalizingly just out of reach.

What exactly do I mean by catharsis? From Wikipedia:

meaning "purification" or "cleansing" derived from the ancient Greek gerund καθαίρειν transliterated as kathairein "to purify, purge," and adjective katharos "pure or clean" (ancient and modern Greek: καθαρός).

How important is catharsis to the RPG experience? I think for any serious ( non-psychopathic) player, the ability to exercise their imagination in an environment that unconditionally accepts the suspension of disbelief and judgment while allowing a drama to unfold that could not otherwise find expression in daily life IS the REWARD. All gain in role playing lives only in the imagination. It is the feeling we take with us after we've played "the Game" that provides the motivation for the true RPGer.

This place we create for ourselves in the imagination that we use for cathartic release is implicitly a place that it meant to be occupied for only a limited time. It is the reality we use to escape from the everyday. It is not meant to replace our the reality of our daily existence. By allowing allowing us to enter periodically, it provides a means to enrich our daily existence by framing our struggles in more metaphorical terms and hopefully providing a means to come to terms with them.

This brings us to the question : Can the computerized RPG experience emulate catharsis that great collective reality based RPGs are capable of rewarding their players with?

I think that despite the increasing sophistication of virtual worlds, they will ultimately be limited by the vision imposed by their game designers. MMORPGs such as World of Warcraft will never be able to provide the essential ingredient for successful RPGs - catharsis. The real crux of the matter is the question of whether or not this is by design. There are are games out there that capture the essence of the RPG (Ico and Shadow of the Colussus come to mind), but WoW is not one of them. Players should question the what master the game serves - is it the players who benefit from being perpetually entrenched in the accumulation of "legendary" rewards, or the company that requires a subscription to enter this virtual world? How does "the Game" subvert our daily lives when so much actual time is required for the commitment of maintaining a virtual life? When does the "the Game" become "Life"?

When you return to reality, I'll be waiting with my dice.

Ground Rules

I'm a big fan of Ondaatje, and as an homage to his work, I'm going to post my own Elimination Dances. What are these you ask? From the back sleeve of my copy of the Bilingual Traveller's Edition :

"Elimination Dance is based on those dances where a caller decides, seemingly randomly, who is forbidden to continue dancing."

The pronouncements are presented in both English and French (this should make the Babelfish translations interesting).

Got your own? Just add them to the comments.

March 24, 2008

Further Eliminations I

Those who are drunk with life.

Ceux qui sont ivres avec la vie.

March 26, 2008

Ruby in OS X Leopard

If you are using Ruby in OS X, I suggest NOT managing it through Fink. OS X comes with Ruby pre-installed. All that is required is to update and install a number of gems as per the Apple Developer Instructions. The instructions also cover configuring Xcode to use for Rails development. After trying both Aptana and the Rails plugin for Eclipse, I suggest the path of least resistance. Xcode is particularly elegant and uncumbersome.

# sudo gem update --system
# sudo gem install rails
# sudo gem update rake
# sudo gem update sqlite3-ruby

Building the PostgreSQL gem requires an additional step as the installed database is managed through Fink (thanks to Roy Hooper fom this one):

# export PATH=/sw/lib/postgresql82/bin/:$PATH
# sudo env ARCHFLAGS="-arch i386" gem install --remote postgres

Modify the environment.rb file to reflect the version of rails installed above. For example, the default scaffold built by the rails command assumes that the version is 2.0.2. To prevent the following error:

Missing the Rails 2.0.2 gem. Please `gem install -v=2.0.2 rails`, 
update your RAILS_GEM_VERSION setting in config/environment.rb for 
the Rails version you do have installed, or comment out RAILS_GEM_VERSION 
to use the latest version installed.

I had to append .9097 to bring the rails version to 2.0.2.9097 for this rails release at the time of writing this article. This will most likely be different for newer versions of rails. The version option for the rails command returns version 2.0.2, but the issuing the following command will return the release number :

# gems list

If you update your gems, you will have to update the environment.rb file with the release version.

I also do not recommend using Aptana. Although it is tempting to use an integrated IDE, there is a bunch of functionality that is buggy (such as the Generators).

About March 2008

This page contains all entries posted to Z1R0 in March 2008. They are listed from oldest to newest.

February 2008 is the previous archive.

April 2008 is the next archive.

Many more can be found on the main index page or by looking through the archives.

Creative Commons License
This weblog is licensed under a Creative Commons License.