postgresql on osx 10.5 with port

$ sudo port install postgresql83-server

でインストールすると、こんなメッセージが出た。

###########################################################
# A startup item has been generated that will aid in
# starting postgresql83-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.postgresql83-server.plist
###########################################################
--->  Installing postgresql83-server 8.3.5_0

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

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

で、早速言う通りに。

$ sudo mkdir -p /opt/local/var/db/postgresql83/defaultdb
$ sudo chown postgres:postgres /opt/local/var/db/postgresql83/defaultdb
$ sudo su postgres -c '/opt/local/lib/postgresql83/bin/initdb -D /opt/local/var/db/postgresql83/defaultdb'
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
could not identify current directory: Permission denied
could not identify current directory: Permission denied
could not identify current directory: Permission denied
The program "postgres" is needed by initdb but was not found in the
same directory as "initdb".
Check your installation.

三つ目で失敗してる。なんで?で調べてみたら、postgresというユーザーが作られたらしいので、そいつでコマンドを実行しないといけないらしい。

$ sudo su - postgres
$ pwd
/opt/local/var/db/postgresql83
$ /opt/local/lib/postgresql83/bin/initdb -D /opt/local/var/db/postgresql83/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 database encoding has accordingly been set to SQL_ASCII.
The default text search configuration will be set to "english".

fixing permissions on existing directory /opt/local/var/db/postgresql83/defaultdb ... ok
creating subdirectories ... ok
selecting default max_connections ... 20
selecting default shared_buffers/max_fsm_pages ... 2400kB/20000
creating configuration files ... ok
creating template1 database in /opt/local/var/db/postgresql83/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/postgresql83/bin/postgres -D /opt/local/var/db/postgresql83/defaultdb
or
    /opt/local/lib/postgresql83/bin/pg_ctl -D /opt/local/var/db/postgresql83/defaultdb -l logfile start

って出たので、

$ /opt/local/lib/postgresql83/bin/postgres -D /opt/local/var/db/postgresql83/defaultdb
LOG:  database system was shut down at 2008-12-23 17:23:27 JST
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

を実行したらサーバーが起動したっぽい。もうちょっと積極的なメッセージが欲しい。不安だ。

^CLOG:  received fast shutdown request
LOG:  aborting any active transactions
LOG:  autovacuum launcher shutting down
LOG:  shutting down
LOG:  database system is shut down

終了するには、Ctrl+CでOKらしい。
デーモンとして?起動するのは、pg_ctlを使う方なんだけど、権限がないので、postgresじゃなくてsudoできるユーザーで以下の準備が必要。

$ sudo touch /opt/local/var/db/postgresql83/logfile
$ sudo chown postgres:postgres /opt/local/var/db/postgresql83/logfile
$ sudo su - postgres
$ /opt/local/lib/postgresql83/bin/pg_ctl -D /opt/local/var/db/postgresql83/defaultdb -l logfile start
server starting

起動したらしい。やっぱり積極性に欠けるメッセージだ。

クライアントで接続しないと本当に使えるのかどうかが不安なのでつないでみる。

$ /opt/local/lib/postgresql83/bin/psql
Welcome to psql 8.3.5, 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

postgres=# 

おおー、繋がったぽい。クライアントは積極的な感じがする。\h, \?でいろいろ出てくるんすね。早速いじってみる。

postgres=# \l
        List of databases
   Name    |  Owner   | Encoding  
-----------+----------+-----------
 postgres  | postgres | SQL_ASCII
 template0 | postgres | SQL_ASCII
 template1 | postgres | SQL_ASCII
(3 rows)

postgres=# create database sandbox;
CREATE DATABASE
sandbox=# \l
        List of databases
   Name    |  Owner   | Encoding  
-----------+----------+-----------
 postgres  | postgres | SQL_ASCII
 sandbox   | postgres | SQL_ASCII
 template0 | postgres | SQL_ASCII
 template1 | postgres | SQL_ASCII
(4 rows)

postgres=# \c sandbox 
You are now connected to database "sandbox".
sandbox=# \dt
No relations found.

sandbox=# create table products (id int primary key, name varchar(200));
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "products_pkey" for table "products"
CREATE TABLE
sandbox=# 
sandbox=# 
sandbox=# \dt
          List of relations
 Schema |   Name   | Type  |  Owner   
--------+----------+-------+----------
 public | products | table | postgres
(1 row)


sandbox=# \q
could not save history to file "/opt/local/var/db/postgresql83/.psql_history": Permission denied
$ 

うわ、これも権限ないのか。まあ、クライアント使う時は普通のユーザーでやれば良いよね。

とりあえず、テーブルまで作れればひとまず安心。

あ、まだサーバーを落としてなかった。

$ /opt/local/lib/postgresql83/bin/pg_ctl -D /opt/local/var/db/postgresql83/defaultdb stop
waiting for server to shut down.... done
server stopped

勘でやったら止まった。よしよし。