MacPorts で PostgreSQL をインストールする

PostgreSQL をインストールする前に依存関係やオプションを確認する

% port variants postgresql84
Password:
postgresql84 has the variants:
   krb5: add support for Kerberos 5 authentication
   perl: add Perl support
   python: add support for python
   tcl: add Tcl support
   universal: Build for multiple architectures

% port deps postgresql84 
Full Name: postgresql84 @8.4.3
Build Dependencies:   bison
Library Dependencies: readline, openssl, zlib, libxml2, libxslt, ossp-uuid

% port variants postgresql84-server 
postgresql84-server has no variants

% port deps postgresql84-server
Full Name: postgresql84-server @8.4.3
Runtime Dependencies: postgresql84

PostgreSQL のクライアントをインストールする

PerlPython のドライバも一緒にインストールする。

% sudo port install postgresql84 +perl +python
--->  Computing dependencies for postgresql84
--->  Fetching bison
--->  Attempting to fetch bison-2.4.2.tar.bz2 from ftp://ftp.dti.ad.jp/pub/GNU/bison
--->  Verifying checksum(s) for bison
--->  Extracting bison
--->  Configuring bison
--->  Building bison
--->  Staging bison into destroot
--->  Installing bison @2.4.2_0
--->  Activating bison @2.4.2_0
--->  Cleaning bison
--->  Fetching ossp-uuid
--->  Attempting to fetch uuid-1.6.2.tar.gz from ftp://ftp.jp.FreeBSD.org/pub/FreeBSD/ports/distfiles/
--->  Verifying checksum(s) for ossp-uuid
--->  Extracting ossp-uuid
--->  Applying patches to ossp-uuid
--->  Configuring ossp-uuid
--->  Building ossp-uuid
--->  Staging ossp-uuid into destroot
--->  Installing ossp-uuid @1.6.2_0
--->  Activating ossp-uuid @1.6.2_0
--->  Cleaning ossp-uuid
--->  Fetching postgresql84
--->  Attempting to fetch postgresql-8.4.3.tar.bz2 from http://ftp9.us.postgresql.org/pub/mirrors/postgresql/source/v8.4.3/
--->  Verifying checksum(s) for postgresql84
--->  Extracting postgresql84
--->  Configuring postgresql84
--->  Building postgresql84
--->  Staging postgresql84 into destroot
--->  Installing postgresql84 @8.4.3_0+perl+python

To use the postgresql server, install the postgresql84-server port
--->  Activating postgresql84 @8.4.3_0+perl+python
--->  Cleaning postgresql84

最後に表示されるメッセージでは、クライアントと一緒にサーバーがインストールされるわけではないので、別途インストールしろとなっている。

To use the postgresql server, install the postgresql84-server port
--->  Activating postgresql84 @8.4.3_0+perl+python
--->  Cleaning postgresql84

PostgreSQL サーバーをインストールする

% sudo port install postgresql84-server
--->  Computing dependencies for postgresql84-server
--->  Fetching postgresql84-server
--->  Verifying checksum(s) for postgresql84-server
--->  Extracting postgresql84-server
--->  Configuring postgresql84-server
--->  Building postgresql84-server
--->  Staging postgresql84-server into destroot
--->  Creating launchd control script
###########################################################
# A startup item has been generated that will aid in
# starting postgresql84-server with launchd. It is disabled
# by default. Execute the following command to start it,
# and to cause it to launch at startup:
#
# sudo launchctl load -w /Library/LaunchDaemons/org.macports.postgresql84-server.plist
###########################################################
--->  Installing postgresql84-server @8.4.3_0

To create a database instance, after install do
 sudo mkdir -p /opt/local/var/db/postgresql84/defaultdb
 sudo chown postgres:postgres /opt/local/var/db/postgresql84/defaultdb
 sudo su postgres -c '/opt/local/lib/postgresql84/bin/initdb -D /opt/local/var/db/postgresql84/defaultdb'

To tweak your DBMS, consider increasing kern.sysv.shmmax by adding an increased kern.sysv.shmmax .. to /etc/sysctl.conf
--->  Activating postgresql84-server @8.4.3_0
--->  Cleaning postgresql84-server

PostgreSQL をデーモンとして起動する場合

サーバーをインストールした時に表示されたメッセージに表示されたコマンドを実行する。

% sudo launchctl load -w /Library/LaunchDaemons/org.macports.postgresql84-server.plist

PostgreSQL の初期化されたデータベースを生成する

サーバーインストール時に表示されるメッセージに記載されたコマンドを実行する。

% sudo mkdir -p /opt/local/var/db/postgresql84/defaultdb
Password:
% sudo chown postgres:postgres /opt/local/var/db/postgresql84/defaultdb

% sudo su postgres -c '/opt/local/lib/postgresql84/bin/initdb -E UTF-8 --no-locale -D /opt/local/var/db/postgresql84/defaultdb'
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale C.
The default text search configuration will be set to "english".

fixing permissions on existing directory /opt/local/var/db/postgresql84/defaultdb ... ok
creating subdirectories ... ok
selecting default max_connections ... 20
selecting default shared_buffers ... 2400kB
creating configuration files ... ok
creating template1 database in /opt/local/var/db/postgresql84/defaultdb/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:

    /opt/local/lib/postgresql84/bin/postgres -D /opt/local/var/db/postgresql84/defaultdb
or
    /opt/local/lib/postgresql84/bin/pg_ctl -D /opt/local/var/db/postgresql84/defaultdb -l logfile start

initdb のオプションは下記のリンク先を参照。

今回、MacPorts で指定されていないオプションは文字コードの初期設定と、OS の locale の設定を使わないことを指定している。
ロケールに関しては、

--no-localeは、ロケールを使用しないことを設定するオプションです。ロケールとは、言語や文化に応じた処理をするOSの機構です。
PostgreSQLでは、このロケールを主にデータのソート処理に使用しますが、英語や日本語のデータを扱う場合には特に必要ありません。反対にロケールを設定すると、ロケール処理の分、ソート処理などが遅くなりますので、注意してください。

http://www.atmarkit.co.jp/fdb/rensai/postgresql/01/pgtune1-2.html

を参考にした。

文字コードは今回、UTF-8 をしている。EUC-JP を指定したい場合は、

% sudo su postgres -c '/opt/local/lib/postgresql84/bin/initdb -E EUC-JP --no-locale -D /opt/local/var/db/postgresql84/defaultdb'

と指定する。
ちなみに、PostgreSQL では Shift-JIS はサポートしていないので、Shift-JIS を指定することはできない。 

PostgreSQL を起動や停止するのに必要なコマンドを利用しやすくする

管理用のコマンドが PATH 外にあるので、シンボリックリンクを PATH 内に作る。

% which postgres
postgres not found

% which pg_ctl
pg_ctl not found

下記のコマンドを実行する。

% ls -l /opt/local/lib/postgresql84/bin/postgres 
-rwxr-xr-x  2 root  admin  4902904  4 13 20:53 /opt/local/lib/postgresql84/bin/postgres

% ls -l /opt/local/lib/postgresql84/bin/pg_ctl 
-rwxr-xr-x  2 root  admin  35488  4 13 20:53 /opt/local/lib/postgresql84/bin/pg_ctl

シンボリックリンクを生成する。

% sudo ln -s /opt/local/lib/postgresql84/bin/postgres /opt/local/bin/
Password:
% sudo ln -s /opt/local/lib/postgresql84/bin/pg_ctl /opt/local/bin/

PostgreSQLコマンドラインから起動・停止をする

まずは、起動する。

% sudo su postgres -c 'pg_ctl -D /opt/local/var/db/postgresql84/defaultdb start'           
server starting
LOG:  database system was shut down at 2010-04-13 22:19:33 JST
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

停止する場合は、下記のコマンドを実行する。

% sudo su postgres -c '/opt/local/bin/pg_ctl -D /opt/local/var/db/postgresql84/defaultdb stop'
waiting for server to shut down.... done
server stopped

PostgreSQL は root ユーザー権限での起動は禁じられているため、postgres ユーザー権限のみの管理が許可されている。
root ユーザーで起動した場合などは、

% sudo postgres -c 'pg_ctl -D /opt/local/var/db/postgresql84/defaultdb -l logfile start'
"root" execution of the PostgreSQL server is not permitted.
The server must be started under an unprivileged user ID to prevent
possible system security compromise.  See the documentation for
more information on how to properly start the server.

とエラーが発生し、起動ができない。当然、停止時も同様にエラーが発生する。

% sudo /opt/local/bin/pg_ctl -D /opt/local/var/db/postgresql84/defaultdb stop
Password:
pg_ctl: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.

PostgreSQL インストールと同時に作成された postgres ユーザー情報を確認する

昔の Mac OS X では、NetInfo で確認をしていたが、Leopard 以降は dscl コマンドで確認をする。

% dscl . -read /Users/postgres
AppleMetaNodeLocation: /Local/Default
GeneratedUID: 34A00C29-80F0-4AF3-A8EA-88560B8EB074
NFSHomeDirectory: /opt/local/var/db/postgresql84
Password: *
PrimaryGroupID: 500
RealName:
 PostgreSQL-84 Server
RecordName: postgres
RecordType: dsRecTypeStandard:Users
UniqueID: 500
UserShell: /bin/sh

% dscl . -read /Groups/postgres
AppleMetaNodeLocation: /Local/Default
GeneratedUID: C79EA9FA-DB6F-4A4A-B25E-F5A9D263BD4F
Password: *
PrimaryGroupID: 500
RealName: postgres
RecordName: postgres
RecordType: dsRecTypeStandard:Groups

su コマンドから postgres ユーザーになってみる

% sudo su - postgres
MacBookPro:~ postgres$ cd ~/
MacBookPro:~ postgres$ pwd
/opt/local/var/db/postgresql84

ホームディレクトリが dscl コマンドで表示された NFSHomeDirectory のディレクトリと同じものになっている。
postgres ユーザーのホームディレクトリには、インストール後に作成したデータベースのインスタンスがある。

MacBookPro:~ postgres$ ls -l
total 0
drwx------  15 postgres  postgres  510 Apr 14 00:21 defaultdb

MacBookPro:~ postgres$ ls -l defaultdb/
total 64
-rw-------   1 postgres  postgres      4 Apr 14 00:21 PG_VERSION
drwx------   5 postgres  postgres    170 Apr 14 00:21 base
drwx------  41 postgres  postgres   1394 Apr 14 00:21 global
drwx------   3 postgres  postgres    102 Apr 14 00:21 pg_clog
-rw-------   1 postgres  postgres   3652 Apr 14 00:21 pg_hba.conf
-rw-------   1 postgres  postgres   1631 Apr 14 00:21 pg_ident.conf
drwx------   4 postgres  postgres    136 Apr 14 00:21 pg_multixact
drwx------   2 postgres  postgres     68 Apr 14 00:21 pg_stat_tmp
drwx------   3 postgres  postgres    102 Apr 14 00:21 pg_subtrans
drwx------   2 postgres  postgres     68 Apr 14 00:21 pg_tblspc
drwx------   2 postgres  postgres     68 Apr 14 00:21 pg_twophase
drwx------   4 postgres  postgres    136 Apr 14 00:21 pg_xlog
-rw-------   1 postgres  postgres  16850 Apr 14 00:21 postgresql.conf

宿題(解決済)

下記のエラーの解決方法がまだわからない。

% sudo su postgres -c 'pg_ctl -D /opt/local/var/db/postgresql84/defaultdb -l logfile start'
Password:
server starting
sh: logfile: Permission denied

上記のエラーが出るのは、ディレクトリに logfile を生成する権限がないためである。logfile は絶対パスを指定すれば、例えば下記のコマンドで正常に起動できる。

% sudo su postgres -c 'pg_ctl -D /opt/local/var/db/postgresql84/defaultdb -l /opt/local/var/db/postgresql84/logfile start'
server starting