PostgreSQL/PostGISのインストール(オープンソース版)
ここではPostGISをオープンソースからコンパイルしてインストールする方法を紹介する。ただし、ベースとなるPostgreSQLは標準パッケージ利用してインストールしている。
■ PostgreSQLのインストール
Ubuntu 9.10の場合、標準パッケージのPostgreSQLが8.4なので、SQLサーバはそのまま標準パッケージをインストールした。
root@postgis:~# apt-get install postgresql
厳密には最新版ではないかも知れないが、ここはそれ程神経質にならず、標準パッケージを使った方が、パッチの対応も“お任せ”できるので良いだろう。
■ PostGISの実行に必要なパッケージの追加
標準パッケージからインストールする場合は、パッケージ管理システム(Debian系だとapt、Reddhat系だとyum等)が必要となる関連パッケージも自動的にインストールするので、余計な手間がかからないが、ソースコードからインストールする場合には、先に必要なパッケージを手動でインストールする必要がある。そのため、PostGIS環境で利用するパッケージ、GEOS(Geometry Engine Open Source:幾何エンジン)とProj.4(Cartographic Projections Library:地図処理投影ライブラリ)を次の様にインストールしておく。
root@postgis:~# apt-get install libgeos-dev root@postgis:~# apt-get install proj
■ PostGISのインストールに必要なパッケージの追加
PostGISのインストール(コンパイル、設定、チェック)に必要なパッケージを予めインストールしておく。
root@postgis:~# apt-get install libpq-dev root@postgis:~# apt-get install postgresql-server-dev-8.4 root@postgis:~# apt-get install flex root@postgis:~# apt-get install xsltproc root@postgis:~# apt-get install libcunit1-ncurses-dev
最後のCUnitパッケージは無くてもインストールは可能。CUnitは、後述する“インストール状況の確認”の際に、CUnitが存在すればユニットテストを行う。もし、CUnitが存在しなければユニットテストは省略される。
次のパッケージはドキュメント生成で必要とされるパッケージのようで、これらをインストールしなくてもPostGISをインストールすることはできる。これらのパッケージ、特にdblatexはフォントデータをインストールするためサイズが大きい。500MB近く喰ってしまうので、例えばPostGISと座標処理だけしたいとかいう場合は必要ないかも知れない。もっとも500MB程度は高が知れているのだけど。)ドキュメントも含め全てをインストールする場合には予め追加しておく。
root@postgis:~# apt-get install graphicsmagick-imagemagick-compat root@postgis:~# apt-get install dblatex root@postgis:~# apt-get install ldp-docbook-xsl
■ PostGISのインストール
今までの作業はパッケージのインストールが中心だったのでroot(つまり sudo -sの状態)で実行した。以降の作業も rootで続けることもできるが、PostGISのソースの保管・管理などは一般ユーザで行った方が良いので、“postgres”というユーザで行うことにした。(“postgis”などのユーザを作成しても良いが、adminユーザに登録したり、データベースに管理者として登録するなど、セキュリティが低下するため“postgres”で作業することにした。この辺りを気にしないのであればpostgresとは別に“postgis”というアカウントを作成して作業した方が良いだろう。)
まず、postgresアカウントをadminグループに追加し、パスワードを設定してから、postgresにsu(switch user)する。
root@postgis:~# adduser postgres admin Adding user `postgres' to group `admin' ... Adding user postgres to group admin Done. root@postgis:~# passwd postgres Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully root@postgis:~# su - postgres To run a command as administrator (user "root"), use "sudo <command>". See "man sudo_root" for details. postgres@postgis:~$
(なお、一連の作業が終わったらpostgresをadminグループから削除し、パスワードも潰しておいた方が良いだろう。)
次にPostGISのソースコードをダウンロードする。“PostGIS:Downloads”ページから適当なものをダウンロードする。私は今日時点で最新版である postgis-1.4.1.tar.gz をダウンロードした。ダウンロードしてtar展開すると以下の様にファイルが解凍される。
postgres@postgis:~$ wget http://postgis.refractions.net/download/postgis-1.4.1.tar.gz postgres@postgis:~$ ls 8.4 postgis-1.4.1.tar.gz postgres@postgis:~$ tar xf postgis-1.4.1.tar.gz postgres@postgis:~$ ls 8.4 postgis-1.4.1 postgis-1.4.1.tar.gz postgres@postgis:~$ ls postgis-1.4.1 aclocal.m4 COPYING liblwgeom postgis TODO autogen.sh CREDITS libtool postgis_comments.sql topology ChangeLog doc loader postgis_config.h.in utils config.guess extras ltmain.sh README.postgis Version.config config.sub GNUmakefile macros regress configure install-sh Makefile spatial_ref_sys.sql configure.ac java NEWS STYLE postgres@postgis:~$
ここでREADME.postgisを見て先ほどインストールしたPostgreSQLのバージョンが適合するか確認しておく。以下の様な記述があるはずだ。
postgres@postgis:~$ more postgis-1.4.1/README.postgis : REQUIREMENTS ------------ PostGIS is compatible with PostgreSQL 8.2 and above. :
問題無ければ、PostGISのソースコードのディレクトリに移り、最初に“./configure”を実行する。無事にコンフィグレーションが実行できれば、次のようなサマリが出て終わる。
postgres@postgis:~/postgis-1.4.1$ ./configure : : PostGIS is now configured for x86_64-unknown-linux-gnu -------------- Compiler Info ------------- C compiler: gcc -g -O2 C++ compiler: g++ -------------- Dependencies -------------- GEOS config: /usr/bin/geos-config GEOS version: 3.1 PostgreSQL config: /usr/bin/pg_config PostgreSQL version: 8.4 PROJ4 version: 46 PostGIS debug level: 0 -------- Documentation Generation -------- xsltproc: /usr/bin/xsltproc xsl style sheets: /usr/share/xml/docbook/stylesheet/nwalsh dblatex: /usr/bin/dblatex convert: /usr/bin/convert
次にライブライブラリのコンパイルやSQLプログラム、コメントを生成する。
postgres@postgis:~/postgis-1.4.1$ make : : PostGIS was built successfully. Ready to install.
“PostGIS was built successfully. Ready to install.”というメッセージが出てくればOK。
次に commnetsデータベースとドキュメント類を作成する。
postgres@postgis:~/postgis-1.4.1$ make comments postgres@postgis:~/postgis-1.4.1$ make docs
作成したライブラリ類をインストールする。
postgres@postgis:~/postgis-1.4.1$ sudo make install postgres@postgis:~/postgis-1.4.1$ sudo make comments-install postgres@postgis:~/postgis-1.4.1$ sudo make docs-install
(makeを実行せずに行き成りmake installでも良いが、コンパイルエラーやインストールエラーが発生した場合に原因の切り分けを単純にするためにもmakeとmake installは分けて実行した方が良い。)
このコマンドで(バージョンや環境等によっても異なるが)次のファイルがインストールされる。
/usr/lib/postgresql/8.4/lib/postgis-1.4.so /usr/share/postgresql/8.4/contrib/spatial_ref_sys.sql /usr/share/postgresql/8.4/contrib/postgis.sql /usr/share/postgresql/8.4/contrib/uninstall_postgis.sql /usr/share/postgresql/8.4/contrib/postgis_upgrade_14_minor.sql /usr/share/postgresql/8.4/contrib/postgis_upgrade_13_to_14.sql /usr/share/postgresql/8.4/contrib/postgis_upgrade_12_to_14.sql /usr/share/postgresql/8.4/contrib/postgis_comments.sql /usr/lib/postgresql/8.4/bin/pgsql2shp /usr/lib/postgresql/8.4/bin/shp2pgsql /usr/share/doc/postgresql/postgis/postgis.html /usr/share/doc/postgresql/postgis/README.postgis /usr/share/postgresql/8.4/man/man1/pgsql2shp.1 /usr/share/postgresql/8.4/man/man1/shp2pgsql.1
ここで注意しなければならないのは、2つのLinuxコマンド pgsql2shpと shp2pgsqlである。標準パッケージからのインストールではこれらのコマンドは /usr/bin/に設置されるので、何の追加設定も無く利用することができる。しかし、/usr/lib/postgresql/8.4/bin/にはpathが設定されていないので、このままでは2つのコマンドを利用できない。正統派(?)の設定方法としては、PostGISを利用するユーザのpathに“/usr/lib/postgresql/8.4/bin”を追加することだろう。しかし、私は今回のケースでは /usr/bin/からシンボリックリンクを張る方法で対処した。
$ sudo ln -s /usr/lib/postgresql/8.4/bin/shp2pgsql /usr/bin/ $ sudo ln -s /usr/lib/postgresql/8.4/bin/pgsql2shp /usr/bin/
将来、パッケージ版のpostgisをインストールした際は上書きされる。
■ インストール状況の確認
最後に make checkにより、正常にインストール出来たかをテスト(リグレッション・テスト)する。
postgres@postgis:~/postgis-1.4.1$ make check : : Run tests: 42 Failed: 0 make[1]: Leaving directory `/var/lib/postgresql/postgis-1.4.1/regress'
“Failed”が0であればOK。
インストール作業が無事に終わったら作業用ディレクトリをお掃除しておこう。
postgres@postgis:~/postgis-1.4.1$ make clean