PostgreSQL のインストールから自動起動まで

1.ユーザ postgres の作成

PostgreSQLの管理者グループと管理者アカウントを作成する。

# groupadd postgres  ## グループは先に作成しておく
# useradd -g postgres postgres
# passwd postgres

2.ソースのダウンロード

PostgreSQLのバージョンはこの時点で最新の8.3.5。

# cd /usr/local
# wget ftp://ftp.sra.co.jp/pub/cmd/postgres/8.3.5/postgresql-8.3.5.tar.gz
# tar xzf postgresql-8.3.5.tar.gz

3.インストールディレクトリの作成

PostgreSQL のインストールディレクトリを作成し、権限を↑で作成したユーザに割り当てる。

# mkdir /usr/local/pgsql
# chown postgres:postgres /usr/local/pgsql
# chown -R postgres:postgres /usr/local/postgresql-8.3.5

4.インストール

ここでは postgres になって作業を行う。

$ su - postgres
$ cd /usr/local/postgresql-8.3.5
$ ./configure --without-readline
$ make
$ make install

5.初期化

postgres になり、~/.bash_profile に以下の設定を記述する。

$ vi ~/.bash_profile
--
PGHOME=/usr/local/pgsql
PGDATA=$PGHOME/data
PGLIB=$PGHOME/lib
PATH=$PATH:$HOME/bin:$PGHOME/bin
export PGHOME PGDATA PGLIB PATH
--

source コマンドで設定を有効にする。

$ . ~/.bash_profile

initdb コマンドを実行し、データベースの初期化を行う。

$ initdb
  :
  :
Success. You can now start the database server using:

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

6.postgresql ライブラリの設定

postgresql のライブラリを他のプログラムから利用できるように、リンカ「ld」にライブラリへのパスを教える。root で、/etc/ld.so.conf を修正する。

include ld.so.conf.d/*.conf
/usr/local/pgsql/lib  # 追加

ld.so.conf から ld.so.cache ファイルを生成し、設定を有効にする。

# ldconfig -v

7.起動、起動確認

postgres になり、postgresql を起動する。

$ pg_ctl -w start

ps コマンドで起動中のプロセスを表示し、postgresql の起動を確認する。

$ su - root
# ps ax | grep postgres

8.自動起動用のスクリプトの作成

起動スクリプトを /etc/init.d/pgsql とする。

# cd /etc/init.d
# vi pgsql
--
#!/bin/sh
#
# chkconfig: 35 86 15
# description: PostgreSQL auto start

PGACCOUNT="postgres"
PGDATA="/usr/local/pgsql/data"
PG_CTL="/usr/local/pgsql/bin/pg_ctl"

. /etc/rc.d/init.d/functions

su - $PGACCOUNT -c "$PG_CTL -D $PGDATA start"
--
# chmod 755 pgsql

9.サービスへの登録

スクリプトを chkconfig に登録し、サービスの設定を行う。

# chkconfig --add pgsql
# chkconfig --level 2345 pgsql on
# chkconfig --list pgsql
pgsql     0:off   1:off   2:on    3:on    4:on    5:on    6:off

10.再起動、自動起動確認

再起動して、プロセスの確認を行う。

# ps ax | grep postgres