Postgresをソースファイルからビルドする

Postgres

Webシステムを構築する上で必要になる作業はいくつもありますが、その中でもデータベースをソースファイルからビルドするという作業は重要なものです。何せパフォーマンスに影響するのですから、 yum install postgresql なんてやっている場合ではないでしょう。ということで、今回は Postgres をソースファイルからビルドする手順をまとめたいと思います。

※詳細なインストール手順については下記の公式ガイドを確認してください。 http://www.postgresql.jp/document/9.1/html/installation.html

※参考になる書籍はこちらです。

今回の環境

今回インストールを行った環境は以下のおとりです。

OS:CentOS 6.3(X86_64) Postgres:9.1.7 インストールディレクトリ:/usr/local/pgsql

実行したコマンドだけ抜き出してみる

今回実行したコマンドと実行ユーザーを抜き出してみました。詳細な説明は必要ないという場合は下記の内容を順次実行することで処理を行えます。

[bash] groupadd postgres useradd -g postgres postgres passwd postgres

パスワードを入力する

cd /usr/local/

取得するソースファイルは検討する

wget http://ftp.postgresql.org/pub/source/v9.1.7/postgresql-9.1.7.tar.gz tar xzf postgresql-9.1.7.tar.gz mkdir /usr/local/pgsql chown postgres:postgres /usr/local/pgsql chown -R postgres:postgres /usr/local/postgresql-9.1.7

あらかじめインストールを行う

yum install -y readline-devel yum install -y zlib-devel

ここからはPostgresユーザーで処理を実行する

make make check make install initdb --encoding=UTF8 --no-locale -D /usr/local/pgsql/data

rootで処理を実行する

cp usr/local/postgresql-9.1.7/contrib/start-scripts/linux /etc/init.d/postgres chmod 755 /etc/init.d/postgres chkconfig --add postgres chkconfig --list postgres service postgres start [/bash]

インストール前の準備

インストールを行う前にユーザーとグループを作成します。また、ビルドするファイルについてもダウンロードし、インストールユーザーに権限を設定します。

なお、ソースコードは下記のURLから取得できます。バージョンについては、必要に応じて選択します。 http://www.postgresql.org/ftp/source/

[bash] [root@base ~]# groupadd postgres [root@base ~]# useradd -g postgres postgres [root@base ~]# passwd postgres ユーザー postgres のパスワードを変更。 新しいパスワード: 新しいパスワードを再入力してください: passwd: 全ての認証トークンが正しく更新できました。 [root@base ~]# cd /usr/local/

[root@base local]# wget http://ftp.postgresql.org/pub/source/v9.1.7/postgresql-9.1.7.tar.gz --2012-12-29 11:19:31-- http://ftp.postgresql.org/pub/source/v9.1.7/postgresql-9.1.7.tar.gz ftp.postgresql.org をDNSに問いあわせています... 217.196.149.55, 67.192.136.133, 204.145.124.244 ftp.postgresql.org|217.196.149.55|:80 に接続しています... 接続しました。 HTTP による接続要求を送信しました、応答を待っています... 200 OK 長さ: 20515102 (20M) [application/octet-stream] `postgresql-9.1.7.tar.gz' に保存中

100%[=====================================================================================================================================>] 20,515,102 1.55M/s 時間 15s

2012-12-29 11:19:47 (1.28 MB/s) - `postgresql-9.1.7.tar.gz' へ保存完了 [20515102/20515102]

[root@base local]# tar xzf postgresql-9.1.7.tar.gz [root@base local]# ls bin etc games include lib lib64 libexec postgresql-9.1.7 postgresql-9.1.7.tar.gz sbin share src tripwire [root@base local]# mkdir /usr/local/pgsql [root@base local]# chown postgres:postgres /usr/local/pgsql [root@base local]# chown -R postgres:postgres /usr/local/postgresql-9.1.7

[/bash]

makeの実行

ここからはpostgresユーザーで処理を行います。make を実行し、正常に終了したら make check を実行します。最後に make install を実行してパッケージをインストールします。

[bash] [postgres@base postgresql-9.1.7]$ ./configure .. [postgres@base postgresql-9.1.7]$ make check .. [postgres@base postgresql-9.1.7]$ make install .. [/bash]

makeを実行するとパッケージがインストールされていないためエラーになることもあります。エラーになった場合には適宜インストールを行います。

  1. readline-devel
  2. zlib-devel

これらのパッケージがインストールされていない状態では下記の通りエラーが発生します。

readlineの場合

[bash] … checking for library containing readline… no configure: error: readline library not found If you have readline already installed, see config.log for details on the failure. It is possible the compiler isn't looking in the proper directory. Use --without-readline to disable readline support. [/bash]

上記の通りエラーが出あたら readline-devel パッケージをインストールする。

[bash] root@base ~]# yum install readline-devel Loaded plugins: downloadonly, fastestmirror, priorities Loading mirror speeds from cached hostile * base: ftp.iij.ad.jp * epel: ftp.iij.ad.jp * extras: ftp.iij.ad.jp * updates: ftp.iij.ad.jp 86 packages excluded due to repository priority protections Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package readline-devel.x86_64 0:6.0-4.el6 will be installed --> Processing Dependency: ncurses-devel for package: readline-devel-6.0-4.el6.x86_64 --> Running transaction check ---> Package ncurses-devel.x86_64 0:5.7-3.20090208.el6 will be installed --> Finished Dependency Resolution

Dependencies Resolved


Package Arch Version Repository Size


Installing:

readline-devel x86_64 6.0-4.el6 base 134 k

Installing for dependencies:

ncurses-devel x86_64 5.7-3.20090208.el6 base 642 k

Transaction Summary


Install 2 Package(s)

Total download size: 776 k Installed size: 2.0 M Is this ok [y/N]: y

Downloading Packages:

(1/2): ncurses-devel-5.7-3.20090208.el6.x86_64.rpm | 642 kB 00:00

(2/2): readline-devel-6.0-4.el6.x86_64.rpm | 134 kB 00:00

Total 4.3 MB/s | 776 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : ncurses-devel-5.7-3.20090208.el6.x86_64 1/2 Installing : readline-devel-6.0-4.el6.x86_64 2/2 Verifying : readline-devel-6.0-4.el6.x86_64 1/2 Verifying : ncurses-devel-5.7-3.20090208.el6.x86_64 2/2

Installed: readline-devel.x86_64 0:6.0-4.el6 Dependency Installed: ncurses-devel.x86_64 0:5.7-3.20090208.el6 Complete! [/bash]

zlibの場合

[bash] … checking for inflate in -lz… no configure: error: zlib library not found If you have zlib already installed, see config.log for details on the failure. It is possible the compiler isn't looking in the proper directory. Use --without-zlib to disable zlib support. [/bash]

こちらも上記の通りエラーが出たら zlib-devel パッケージのインストールを行う。

[bash] [root@base ~]# yum install zlib-devel Loaded plugins: downloadonly, fastestmirror, priorities Loading mirror speeds from cached hostile * base: ftp.iij.ad.jp * epel: ftp.iij.ad.jp * extras: ftp.iij.ad.jp * updates: ftp.iij.ad.jp 86 packages excluded due to repository priority protections Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package zlib-devel.x86_64 0:1.2.3-27.el6 will be installed --> Finished Dependency Resolution

Dependencies Resolved


Package Arch Version Repository Size

Installing: zlib-devel x86_64 1.2.3-27.el6 base 44 k

Transaction Summary

Install 1 Package(s)

Total download size: 44 k Installed size: 115 k Is this ok [y/N]: y Downloading Packages: zlib-devel-1.2.3-27.el6.x86_64.rpm | 44 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : zlib-devel-1.2.3-27.el6.x86_64 1/1 Verifying : zlib-devel-1.2.3-27.el6.x86_64 1/1

Installed: zlib-devel.x86_64 0:1.2.3-27.el6

Complete! [/bash]

エラーが表示されない状態で処理が終了したらインストールは完了です。

Postgresの初期化処理

次にPostgresを利用できるように初期化処理を行います。

初期化処理で重要な事は、encodingを指定することとデータディレクトリを指定することです。エンコーディングを正しく指定しないと日本語の扱いがうまくいかなくなりますので注意が必要です。 詳細についてはこちらでわかりやすく説明がされています。

ロケール(国際化と地域化) — Let's Postgres

[bash] [postgres@base ~]$ initdb --encoding=UTF8 --no-locale -D /usr/local/pgsql/data 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".

creating directory /usr/local/pgsql/data ... ok creating subdirectories ... ok selecting default max_connections ... 100 selecting default shared_buffers ... 32MB 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 collations ... ok creating conversions ... ok creating dictionaries ... ok setting privileges on built-in objects ... ok creating information schema ... ok loading PL/pgSQL server-side language ... 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

[/bash]

これでPostgresの初期化まで完了しました。メッセージに表示されているとおりのコマンドを実行することでPostgresが動作することを確認します。

[bash] [postgres@base ~]$ pg_ctl -D /usr/local/pgsql/data -l /usr/local/pgsql/server.log start server starting [/bash]

起動スクリプトの設定

最後に起動スクリプトを設定します。起動スクリプトを設定することでOS起動時に自動的にPostgresを起動することが可能になります。

起動スクリプトソースコードが保存されているファイルに保存されているものをそのまま使用します。

[bash] [root@base contrib]# cp usr/local/postgresql-9.1.7/contrib/start-scripts/linux /etc/init.d/postgres [root@base contrib]# chmod 755 /etc/init.d/postgres [root@base contrib]# chkconfig --add postgres [root@base contrib]# chkconfig --list postgres postgres 0:off 1:off 2:on 3:on 4:on 5:on 6:off

あらかじめ起動したPostgresプロセスは終了する

[root@base contrib]# service postgres start Starting PostgreSQL: ok [/bash]

これでPostgresの起動にかかる設定は完了です。

最後に...

ここまでの設定を行うために必要になった時間ですが、おおむね2時間程度でした。makeで時間がかかったこととドキュメントをまとめながら作業をしたことから時間がかかってしまいました。ただ、なれれば1時間程度で十分作業は完了すると思いますので、面倒くさがらずにソースファイルからmakeするようにしたいと思います。

なお、この後はPostgresのパフォーマンスやアクセス関連の設定を行う必要がありますが、それはまた別な機会にしたいと思います。