LabMemo

Icon

Leopardでテスト環境用にPostgreSQLをインストール

5年くらいまえ、メインマシンがまだThinkPadだった頃、PowerBookにPostgreSQLをインストールする時はADCの資料を参考にFinkの手を借りるお手軽な方法を選んだ。今ならさしずめMac Portsと言うところだろう。

当時、自分にとってのPowerBookは、Vista後を考えた上でメイン環境をOS Xへ移行することを見据えたウオーミングアップ期間であり、配付システムでアップデートを続けても特に気にならないくらいにディスクの空き容量は潤沢にあった。環境がおかしくなってもすべてクリーン・インストールしなおせば良いというお気楽な考えもあたまの中にあった。

しかし、いま使っているMacBook Proはメインマシン。使いもしないソースをわざわざローカルに置いておきたいはずもない。だから今回、PostgreSQLをインストールする際にもMac Portsを使おうといういう考えははなからなかった。クリアにしておきたかったポイントは以下のとおり:

  1. 最新のソースからのインストール
  2. 「postgres」ユーザを利用せず、現在自分が利用しているユーザでPostgreSQLを動かす
  3. 上記も踏まえdsclコマンドでの操作も行わない
  4. 上述のポイントはオーバーラップする部分がある。バイナリで配付されているPostgreSQLでは「postgres」ユーザが作成されてしまう。またユーザが作成されてしまうのでdsclコマンドでユーザ環境の初期操作も必要になる。しかもユーザが増えると、起動時にGUIログインでの画面が現れるという悪循環が生まれる。

    そもそも今回PostgreSQLをローカルへ放り込むことになった理由はデータベースで遊ぶためではなく、Djangoでの開発を行うためなので、このような渾沌がメインマシンに持ち込まれることは精神的にもよくない。

    これらの理由も踏まえて、いろいろと調べることから始めた。この3年ばかりは翻訳仕事がメインになっていたこともあり、正直いって状況把握をかなり怠っていたからだ。

    まずは把握すべくは初期の共有メモリの値。これへの対応はPostgreSQL 8.2.13 Documentationの16.4. Managing Kernel ResourcesAndre Lewis氏のブログエントリを参考にした。

    In OS X 10.3.9 and later, instead of editing /etc/rc you may create a file named /etc/sysctl.conf, containing variable assignments such as

    kern.sysv.shmmax=4194304
    kern.sysv.shmmin=1
    kern.sysv.shmmni=32
    kern.sysv.shmseg=8
    kern.sysv.shmall=1024
    This method is better than editing /etc/rc because your changes will be preserved across system updates. Note that all five shared-memory parameters must be set in /etc/sysctl.conf, else the values will be ignored.

    Beware that recent releases of OS X ignore attempts to set SHMMAX to a value that isn’t an exact multiple of 4096.

    SHMALL is measured in 4 kB pages on this platform.

    実際に割当てた値は以下のとおり:

    % cat /etc/sysctl.conf
    kern.sysv.shmmax=167772160
    kern.sysv.shmmin=1
    kern.sysv.shmmni=32
    kern.sysv.shmseg=8
    kern.sysv.shmall=65536

    sysctl.confを作成した後、マシンを再起動し割当てを有効にして、PostgreSQLをインストール。インストールの方法はなんら悩むようなものではなく、ダウンロードしてきたソースを展開後、素直に./configure、make 、make installの順番で進めていくだけだ。

    「postgres」ユーザの問題はPostgreSQLインストール後のdata/ディレクトリの所有権を自分に変更することで対応している。

    # chown -R egg /usr/local/pgsql/data

    環境変数に実行パスとaliasを追加(私はtcshを使っているので、必要に合わせて読み替ええてほしい)。

    % cat ./.cshrc
    set path=($path /usr/local/bin /opt/local/bin /opt/local/sbin ~/bin /usr/local/pgsql/bin)
    …中略…
    alias pgrun ‘/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l /usr/local/pgsql/data/logfile start’
    alias pgstop ‘/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data stop’

    サーバ機ではないので、起動時にPostgreSQLが立ち上がる必要はないためaliasを使ってパラメータ込みで任意に起動/停止できるようにしてある。

    続いてデータベースのイニシャライズ。

    % initdb -D /usr/local/pgsql/data
    The files belonging to this database system will be owned by user “****”.
    This user must also own the server process.

    The database cluster will be initialized with locales
    COLLATE: ja_JP
    CTYPE: ja_JP.UTF-8
    MESSAGES: ja_JP
    MONETARY: ja_JP
    NUMERIC: ja_JP
    TIME: ja_JP
    The default database encoding has accordingly been set to UTF8.
    initdb: could not find suitable text search configuration for locale ja_JP.UTF-8
    The default text search configuration will be set to “simple”.

    fixing permissions on existing directory /usr/local/pgsql/data … ok
    creating subdirectories … ok
    selecting default max_connections … 100
    selecting default shared_buffers/max_fsm_pages … 32MB/204800
    creating configuration files … ok
    creating template1 database in /usr/local/pgsql/data/base/1 … ok
    initializing pg_authid … ok
    initializing dependencies … ok
    creating system views … ok
    loading system objects’ descriptions … ok
    creating conversions … ok
    creating dictionaries … ok
    setting privileges on built-in objects … ok
    creating information schema … ok
    vacuuming database template1 … ok
    copying template1 to template0 … ok
    copying template1 to postgres … ok

    WARNING: enabling “trust” authentication for local connections
    You can change this by editing pg_hba.conf or using the -A option the
    next time you run initdb.

    Success. You can now start the database server using:

    postgres -D /usr/local/pgsql/data
    or
    pg_ctl -D /usr/local/pgsql/data -l logfile start

    ====================================================
    % postmaster -D /usr/local/pgsql/data
    LOG: database system was shut down at 2009-04-12 00:15:49 JST
    LOG: database system is ready to accept connections
    LOG: autovacuum launcher started
    ====================================================

    % pg_ctl -D /usr/local/pgsql/data stop
    waiting for server to shut down…. done
    server stopped

    さて、これでお膳立ては全部そろった。後はpsycopg2を放り込んで、Djangoと連携させるだけだ。

Category: Leopard

Tagged: ,

Comments are closed.

Categories

Top news@sfgate.com

    First of all …

    First of all, I'd like to say thank you to Mr. Derek Punsalan, who generously shared this great theme. I'm very happy to have a chance to use this simple yet well designed Wordpress theme.

    And of course, I shoud not forget about to say thank you to Wordpress developer team. I'm always admiring open-source developer's incredible effort to open up concealed black box like proprietary culture to create more harmonious and happier future.

    Flickr

    www.flickr.com
    This is a Flickr badge showing public photos and videos from titanium22. Make your own badge here.