さて、必要なツールを準備することができたなら、いよいよPostgreSQLをインストールすることにしましょう。PostgreSQLは、カリフォルニア大学バークレー校で開発された、Postgresと呼ばれるデータベースを基にして作成されています。最新バージョンは「7.0.2」で、2000年6月7日にリリースされたばかりです。
 もちろん、オープンソースですから無償で利用することができるのですが、日本ではSRAが商用サポートを行なっています。オラクルなど商用のデータベースを使うまでもないけど、サポートは受けられるようにしておきたい場合などにも対応できるようになってきているのは、昨今のLinux市場を支える大きな力になっています。
 ソースコードの圧縮されたファイルは、「ftp://ftp.sra.co.jp/pub/cmd/postgres/7.0.2/postgresql-7.0.2.tar.gz」や、Ring Server Projectの各サーバーなどから入手することができます。物理的に近いサーバーを探して入手するといいでしょう。容量は約7.2MBとなっています。
 ファイルを入手してからでもかまいませんが、PostgreSQL用のユーザーを作っておくほうがいいでしょう。このユーザーは、PostgreSQLのオーナーとなるユーザーです。ほかのユーザーと兼ねてもかまわないのですが、専用のユーザーとしておくほうが、なにかと便利だと思います。筆者は「postgres」というユーザーを作成しました。今後、記事の中ではこのユーザーを使って作業を進めていきますので、違う名前のユーザーで作業をする方は、ユーザー名の部分を読み替えて行なってください。
 ユーザーを追加するコマンドは以下の通りです。
         
          [root@cinderella /home]# useradd -d /home/postgres -s /bin/bash 
            -p postgres postgres | 
        
      
      
 ファイルを入手することができたら、そのファイルを適当なディレクトリ(/usr/local/srcなど)に置いて、tarを使って解凍します。この作業は、さきほどのpostgresユーザーで実行していきますが、通常は/usr/local/srcに書きこみ権限がありませんので権限を与えます。rootユーザーでログインして、以下の作業を行なってください。この作業で、PostgreSQLのソースファイルを解凍するディレクトリと、インストールするディレクトリを作成しています。そして、そのディレクトリにPostgresユーザーで書き込みを行なえるよう、ディレクトリの所有者を変更しています。
         
          [root@cinderella src]# mkdir postgresql-7.0.2 
            [root@cinderella src]# chown postgres postgresql-7.0.2 
            [root@cinderella src]# mkdir /usr/local/pgsql 
            [root@cinderella src]# chown postgres /usr/local/pgsql | 
        
      
      
ファイルを解凍するコマンドは以下の通りです。
         
          [postgres@cinderella src]$ tar zxvf postgresql-7.0.2.tar.gz | 
        
      
      
 このコマンドでエラーが出るようなら、tarがGNUの提供するtarでないのかもしれません。その場合にはgzip(gunzip)を使って、ファイルを解凍してから、tarで解凍してください。その場合のコマンドは以下の通りです。
         
          [postgres@cinderella src]$ gunzip -c postgresql-7.0.2.tar.gz 
            | tar xf - | 
        
      
      
 解凍が終わると、postgresql-7.0.2ディレクトリの中に、いくつかのディレクトリができているはずです。ここからの作業は、その中のsrcディレクトリで行ないます。「cd src」と入力して、ディレクトリを移動しておきます。
 次に行う作業は、コンパイル前の設定です。この設定作業では、インストールするディレクトリや、ODBCの要不要などを決定します。デフォルトのままでかまわない項目は、指定する必要がありません。基本的には、日本語を利用することができるように、以下のような設定を行なうといいでしょう。
         
          [postgres@cinderella src]$ ./configure --enable-multibyte | 
        
      
      
 この他の設定用パラメータは、「./configure --help」とするか、postgresql-7.0.2ディレクトリにある「INSTALL」ファイルを見ていただくと確認することができます。
 設定を開始すると、環境を判断して、自動的に各種の設定が行なわれていくようすを確認できます。なにごともなければ、linkを終えてメッセージが終了します。逆に、うまくいっていないと、警告のメッセージが最後に表示されます。参考までに、筆者が実行したログをこちら(postgresql.log)で公開しておきます。
 設定作業の後はコンパイルを行ないます。コンパイルを行なうには、そのままのディレクトリ(/usr/local/src/postgresql-7.0.2/src)で、makeと入力するだけです。コンピュータの性能にもよりますが、この作業には数分から10分程度必要です。ちなみに、この作業は何度でも実行することが可能です。作業を始める前の状態に戻したければ、/usr/local/srcの中を削除してきれいにするだけで、元通りにすることもできます。
         
          [postgres@cinderella src]$ make | 
        
      
      
 この部分のログは、あまりにも長大ですので公開することはしませんが、最後に以下のようなメッセージが表示されていれば問題ありません。
         
          All of PostgreSQL is successfully made. Ready to install. | 
        
      
      
 なんの問題もなければ、いよいよインストールです。インストールを行なうには、以下のように「make install」と入力します。このコマンドを入力すると、コンパイルしたバイナリファイルが、指定されているディレクトリ(デフォルトは/usr/local/pgsql)にコピーされます。この作業は、1分もかからないうちに終了する作業です。無事に終了すれば、以下のようなメッセージが表示されます。
         
          Thank you for choosing PostgreSQL, the most advanced open 
            source database engine. | 
        
      
      
 無事にインストールされているか心配な方は、/usr/local/pgsqlに移動して、ファイルを確認してみるといいでしょう。いくつかのディレクトリが作成されていて、その中にpostgreSQLを構成するファイルがインストールされていることが確認できるはずです。
 これでインストールも終わり、PostgreSQLが使える状態になりましたが、作業完了というにはまだ不充分です。なぜなら、PostgreSQLはデータベースアプリケーションですから、データベースを作っておかなくてはなりません。それに、このままではPostgreSQLを利用するためのツールを呼び出すにも、不自由でなりません。
 まずは、PostgreSQLの各種ツールを、どこからでも実行できるように、コマンドサーチパス(以下パス)を設定することから始めましょう。利用しているシェルによって、設定を行なうファイルが異なりますが、いずれにしても/usr/local/pgsql/bin(デフォルトのディレクトリにインストールした場合)にパスを設定します。
 bashの場合は、ユーザーのホームディレクトリにある「.bashrc」に以下のような1行を追加します。
         
          PATH="$PATH":/usr/local/pgsql/bin | 
        
      
      
 cshとtcshの場合は、ユーザーのホームディレクトリにある「.cshrc」に以下のような1行を追加します。
         
          setenv PATH "$PATH":/usr/local/pgsql/bin | 
        
      
      
 また、同様にいくつかの環境変数も設定します。これらの環境変数も、パスと同様に設定していなくとも困ることはありませんが、設定していないと使い勝手が悪くて不便です。先ほどと同じファイルに、以下のような行を追加して、環境変数の設定を行ないます。
         
          bashの場合: 
            export POSTGRES_HOME=/usr/local/pgsql 
            export PGLIB=$POSTGRES_HOME/lib 
            export PGDATA=$POSTGRES_HOME/data 
            export MANPATH="$MANPATH":$POSTGRES_HOME/man 
            export LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIB" 
             
            cshとtcshの場合: 
            setenv POSTGRES_HOME /usr/local/pgsql 
            setenv PGLIB $POSTGRES_HOME/lib 
            setenv PGDATA $POSTGRES_HOME/data 
            setenv MANPATH "$MANPATH":$POSTGRES_HOME/man 
            setenv LD_LIBRARY_PATH "$LD_LIBRARY_PATH":"$PGLIB" | 
        
      
      
 ここで追加した各行の意味は、以下の通りです。
- POSTGRES_HOME PostgreSQLがインストールされているディレクトリ
 
- PGLIB PostgreSQLのライブラリがインストールされているディレクトリ
 
- PGDATA PostgreSQLで作成するデータベースファイルを格納するディレクトリ
 
- MANPATH PostgreSQLのオンラインマニュアルがインストールされているディレクトリ。これを設定すると、manコマンドでPostgreSQLのオンラインマニュアルが参照できる
 
- LD_LIBRARY_PATH PostgreSQLのクライアント用sharedライブラリがインストールされているディレクトリ(通常はPGLIBと同じディレクトリとなる)
 
 これらの環境設定は、環境設定ファイル(.bashrc/.cshrc)を再読み込みするか、一度ログアウトして再度ログインするまで有効になりません。環境設定ファイルを再読み込みするには、sourceコマンドを利用します。「source ~/.bashrc」もしくは、「source ~/.cshrc」と入力して再読み込みしてください。設定が反映されていることは、envコマンドで確認することができます。
 環境設定に続いて、PostgreSQLデータベースを初期化します。この作業は、インストール後に一度だけ行うものです。いくつかのデータベースを作成するとしても、何度も実行する必要はありませんから、注意してください。この作業を実施すると、PGDATAに設定したディレクトリ以下に、データベース領域が初期設定されます。実行するコマンドは以下の通りです。実行すると、いくつかのメッセージが表示されて、データベースを起動する準備が整います。
         
          [postgres@cinderella postgres]$ initdb | 
        
      
      
 PostgreSQLのデータベースデーモン(postmaster)を起動します。この作業は以下のコマンドで実行できますが、サーバを再起動するたびにデーモンの起動を行なうのは非常に面倒です。そこで、サーバの起動時にPostgreSQLが自動的に起動するよう、以下のコマンドをシステム起動用スクリプトに記述しておくといいでしょう。
         
          [postgres@cinderella data]$ postmaster -S -i | 
        
      
      
 このコマンドが実行されると、サーバのプロセスに「postmaster -S -i」というプロセスの存在が確認できます。
postgresql.log
 
loading cache ./config.cache 
checking host system type... i686-pc-linux-gnu 
checking echo setting... 
checking setting template to... linux_i386 
checking whether to support locale... disabled 
checking whether to support cyrillic recode... disabled 
checking whether to support multibyte... enabled 
checking setting DEF_PGPORT... 5432 
checking setting DEF_MAXBACKENDS... 32 
checking setting USE_TCL... disabled 
checking setting USE_PERL... disabled 
checking setting USE_ODBC... disabled 
checking setproctitle... disabled 
checking setting ASSERT CHECKING... disabled 
checking for gcc... gcc 
checking whether the C compiler (gcc -O2 ) works... yes 
checking whether the C compiler (gcc -O2 ) is a cross-compiler... no 
checking whether we are using GNU C... yes 
checking whether gcc accepts -g... yes 
checking how to run the C preprocessor... gcc -E 
checking whether gcc needs -traditional... no 
- setting CPPFLAGS= 
- setting LDFLAGS= 
checking setting debug compiler flag... using default 
checking for c++... c++ 
checking whether the C++ compiler (c++   ) works... yes 
checking whether the C++ compiler (c++   ) is a cross-compiler... no 
checking whether we are using GNU C++... yes 
checking whether c++ accepts -g... yes 
checking for include  in C++... yes 
checking for namespace std in C++... yes 
checking for a BSD compatible install... /usr/bin/install -c 
checking for flex... flex 
checking for yywrap in -lfl... yes 
checking whether ln -s works... yes 
checking whether make sets ${MAKE}... yes 
checking for ranlib... ranlib 
checking for find... /usr/bin/find 
checking for tar... /bin/tar 
checking for split... /usr/bin/split 
checking for etags... no 
checking for xargs... /usr/bin/xargs 
checking for gzcat... no 
checking for zcat... /bin/zcat 
checking for perl... perl 
checking for bison... /usr/bin/bison 
- Using /usr/bin/bison -y -d 
checking for main in -lsfio... no 
checking for main in -lncurses... yes 
checking for main in -ltermcap... yes 
checking for main in -lreadline... yes 
checking for using_history in -lreadline... yes 
checking for main in -lbsd... yes 
checking for main in -lutil... yes 
checking for main in -lm... yes 
checking for main in -ldl... yes 
checking for main in -lsocket... no 
checking for main in -lnsl... yes 
checking for main in -lipc... no 
checking for main in -lIPC... no 
checking for main in -llc... no 
checking for main in -ldld... no 
checking for main in -lln... no 
checking for main in -lld... no 
checking for main in -lcompat... no 
checking for main in -lBSD... no 
checking for main in -lcrypt... yes 
checking for main in -lgen... no 
checking for main in -lPW... no 
checking for ANSI C header files... yes 
checking for sys/wait.h that is POSIX.1 compatible... yes 
checking for arpa/inet.h... yes 
checking for crypt.h... yes 
checking for dld.h... no 
checking for endian.h... yes 
checking for float.h... yes 
checking for fp_class.h... no 
checking for getopt.h... yes 
checking for history.h... no 
checking for ieeefp.h... no 
checking for limits.h... yes 
checking for netdb.h... yes 
checking for netinet/in.h... yes 
checking for readline.h... no 
checking for readline/history.h... yes 
checking for readline/readline.h... yes 
checking for sys/select.h... yes 
checking for termios.h... yes 
checking for unistd.h... yes 
checking for values.h... yes 
checking for sys/param.h... yes 
checking for pwd.h... yes 
checking for working const... yes 
checking for inline... inline 
checking for preprocessor stringizing operator... yes 
checking for uid_t in sys/types.h... yes 
checking for mode_t... yes 
checking for off_t... yes 
checking for size_t... yes 
checking whether time.h and sys/time.h may both be included... yes 
checking whether struct tm is in sys/time.h or time.h... time.h 
checking for tm_zone in struct tm... yes 
checking for signed types... yes 
checking for volatile... yes 
checking for type of last arg to accept... size_t 
checking for int timezone... yes 
checking for gettimeofday args... 2 args 
checking for union semun... no 
checking for fcntl(F_SETLK)... no 
checking for 8-bit clean memcmp... yes 
checking return type of signal handlers... void 
checking for vprintf... yes 
checking for memmove... yes 
checking for sysconf... yes 
checking for sigprocmask... yes 
checking for waitpid... yes 
checking for setsid... yes 
checking for fcvt... yes 
checking for fpclass... no 
checking for fp_class... no 
checking for fp_class_d... no 
checking for class... no 
checking for snprintf... yes 
checking for vsnprintf... yes 
checking for isinf... yes 
checking for getrusage... yes 
checking for srandom... yes 
checking for gethostname... yes 
checking for random... yes 
checking for inet_aton... yes 
checking for strerror... yes 
checking for strdup... yes 
checking for strtol... yes 
checking for strtoul... yes 
checking for strcasecmp... yes 
checking for cbrt... yes 
checking for rint... yes 
checking for filename_completion_function... yes 
checking for getopt_long... yes 
checking for finite... yes 
checking for sigsetjmp... yes 
checking whether 'long int' is 64 bits... no 
checking whether 'long long int' is 64 bits... yes 
checking whether snprintf handles 'long long int' as %lld... yes 
checking alignment of short... 2 
checking alignment of int... 4 
checking alignment of long... 4 
checking alignment of long long int... 4 
checking alignment of double... 4 
checking for POSIX signal interface... yes 
checking for tclsh... /usr/bin/tclsh 
updating cache ./config.cache 
creating ./config.status 
creating GNUmakefile 
creating Makefile.global 
creating backend/port/Makefile 
creating backend/catalog/genbki.sh 
creating backend/utils/Gen_fmgrtab.sh 
creating bin/pg_dump/Makefile 
creating bin/pg_version/Makefile 
creating bin/pgtclsh/mkMakefile.tcldefs.sh 
creating bin/pgtclsh/mkMakefile.tkdefs.sh 
creating bin/psql/Makefile 
creating include/version.h 
creating interfaces/libpq/Makefile 
creating interfaces/ecpg/lib/Makefile 
creating interfaces/ecpg/preproc/Makefile 
creating interfaces/libpq++/Makefile 
creating interfaces/libpgeasy/Makefile 
creating interfaces/libpgtcl/Makefile 
creating interfaces/odbc/GNUmakefile 
creating interfaces/odbc/Makefile.global 
creating pl/plpgsql/src/Makefile 
creating pl/plpgsql/src/mklang.sql 
creating pl/tcl/mkMakefile.tcldefs.sh 
creating test/regress/GNUmakefile 
creating include/config.h 
linking ./backend/port/dynloader/linux.c to backend/port/dynloader.c 
linking ./backend/port/dynloader/linux.h to include/dynloader.h 
linking ./include/port/linux.h to include/os.h 
linking ./makefiles/Makefile.linux to Makefile.port 
linking ./backend/port/tas/dummy.s to backend/port/tas.s 
linking ./include/port to interfaces/odbc/port 
linking ./makefiles to interfaces/odbc/makefiles 
linking ./template to interfaces/odbc/template 
linking ./include/config.h to interfaces/odbc/config.h |