おたむはうぇぶじぇね

ウェブ制作会社に勤めるおたむの創作備忘録

RedHat el4 に PostgreSQL8.2.5 をソースインストール

Server > PostgreSQL

07.09.22 03:22

RedHat el4 に PostgreSQL8.2.5 をソースでインストールしましょう。
ホスティング会社がセットアップしてくれる PostgreSQL は rpm でズゴーンと入っていてバージョンが 7 系なので、いつもそいつを削除してからソースインストールしています。
そろそろ手順書を残しておこうかね。


まず、root で作業します。
rpm の PostgreSQL を消していきましょう。依存関係にあるものからブチブチと。
消しすぎて他のアプリケーションが動かなくなっても、知りません。依存関係をよく把握してから行ってください。

$ rpm -e postgresql-devel
$ rpm -e postgresql-pl
$ rpm -e postgresql-test
$ rpm -e postgresql-server
$ rpm -e postgresql-contrib
$ rpm -e postgresql-docs
$ rpm -e freeradius-postgresql
$ rpm -e postgresql

次に postgres ユーザを作成します。
ソースを取ってきます。
そして解凍。
オーナーを postgres にチェンジ。
postgres にスイッチし
移動。

$ useradd postgres
$ wget ftp://ftp.jp.postgresql.org/v8.2.5/postgresql-8.2.5.tar.gz
$ tar zxf postgresql-8.2.5.tar.gz
$ chown -R postgres. postgresql-8.2.5
$ su - postgres
$ cd postgresql-8.2.5

そして configure です。
以前指定していたオプション multibyte などが ignore されてしまうのでもうシンプルにいきます。
configure が終わったら make、make install とテンポよく作業を進めます。make install でエラーがでたのなら、それはきっとパーミッションによるファイルが書き込めないというエラーかと思われます。make を終えた時点で root に スイッチし、make install を実行してください。
make install が無事終わったら、/usr/local/pgsql にシンボリックリンクを作成しましょう。

$ ./configure --prefix=/usr/local/postgresql-8.2.5
$ make
$ make install
$ ln -s /usr/local/postgresql-8.2.5 /usr/local/pgsql

次ここ重要。よく忘れる。こいつをやっておかないと、PHP の make test でライブラリが見当たらないと言われてしまう。当然、PHP から pg 系の関数も利用することができない。


ライブラリの登録。
そして ldconfig コマンドの実行。

vi /etc/ld.so.conf

/usr/local/pgsql/lib # この行を追記

$ /sbin/ldconfig

そして postgres ユーザの .bashrc に下記を追記し、環境変数を有効にしてあげましょう。

$ vi ~/.bashrc

PG=/usr/local/pgsql
PATH="$PATH":$PG/bin
export MANPATH="$MANPATH":$PG/man
export PGLIB=$PG/lib
export PGDATA=$PG/data

保存して、有効にする。

$ source ~/.bashrc

インストールが終了したら忘れないうちに、起動スクリプトを作成しましょう。
いったん root にスイッチし、起動スクリプトのサンプルファイルを /etc/init.d/ へコピー。
実行権限を付加。
ランレベルを設定。

$ cp /usr/local/src/postgresql-8.2.5/contrib/start-scripts/linux \
/etc/init.d/postgresql
$ chmod +x /etc/init.d/postgresql
$ chkconfig --add postgresql
$ chkconfig postgresql on

次はまた postgres での作業。postgres ユーザにスイッチしてデータベースファイルを作成。

$ su - postgres

$ cd /usr/local/pgsql/
$ mkdir data
$ initdb --encoding=UTF-8 -D /usr/local/pgsql/data/

いよいよサービスの起動、テストデータベースを作成してみましょう。

$ pg_ctl -D /usr/local/pgsql/data -l /usr/local/pgsql/logfile -o "-i" start
$ createdb -E UTF-8 testdb
CREATE DATABASE
$ psql testdb

> \q

CREATE DATABASE とでるとホッとします。
ここまででエラーが吐かれなければ問題無しです。
つぎに PostgreSQL 専用のユーザ pgadmin を作成しましょう。

$ createuser pgadmin
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) n
Shall the new role be allowed to create more new roles? (y/n) n
CREATE ROLE

CREATE ROLE とでるとホッとします。
ここではなんでもできてしまうスーパーユーザでなく
無作為にデータベースを作成できてしまうユーザでなく
他のユーザなんて作る権限を与えないユーザを作成するため、すべて n として、PostgreSQL 専用のユーザ pgadmin を作成しました。これはそれぞれのサーバポリシーに沿って決定してもらえばよいでしょう。
そしてなんの権限も与えてない pgadmin ですが、パスワードは設定しましょう。

$ psql testdb

> alter user "pgadmin" password '(適当なパスワード)';

ALTER ROLE

> \q

最後は /usr/local/pgsql/data/pg_hba.conf ファイルの trusted の部分を md5 に書き換えて、よりセキュアなデータベースに仕上げましょう。

# TYPE DATABASE USER CIDR-ADDRESS METHOD

# "local" is for Unix domain socket connections only
#local all all trust
local all all md5
# IPv4 local connections:
#host all all 127.0.0.1/32 trust
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 trust

設定を反映させるため、サービスを再起動します。

$ pg_ctl stop
$ pg_ctl -D /usr/local/pgsql/data -l /usr/local/pgsql/logfile -o "-i" start

これ以降は psql コマンドを利用する際に、パスワードの入力が求められます。

$ psql -U pgadmin testdb
Password for user pgadmin:
Welcome to psql 8.2.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

testdb=>

以上で設定完了。
あとは好きにデータをぶっ込んでいきましょう。


次のエントリーでは PHP5.2.5 のインストールを残します。

Tags :: , , ,

Author :: おたむ