PostGIS用データベースの作成
以上までの手順で(標準パッケージからインストールするか、オープンソースからインストールするかに関わらず)PostGISを利用する準備が出来た。次は、GIS情報を格納するためにPostGIS機能拡張されたデータベースを作成する。
PostGIS用データベースを作成するは、
の2通りがある。テンプレート“template_postgis”がPostgreSQLサーバに既に登録されている場合は、
postgres@postgis:~$ createdb -T template_postgis gistestdb
とコマンド1つで済む。(ここで作成するPostGIS用データベースの名前は“gistestdb”とする。以下、同様。) ただ、私のインストールしたPostGISではテンプレートデータベース template_postgisが見当たらなかった。マニュアルを見るとWin32用のインストーラーでバージョン1.1.5以上に入っているようだが、私のインストールしたLinux版にはことごとく入っていなかった。(なぜ、Windows版だけなのだろう?)ダウンロードした“postgis-1.4.1/extras/template_gis”というディレクトリにテンプレート・データベースを作成するシェルスクリプト等が入っているし、“templategis”というmakeコマンドのターゲットも存在するが、“make templategis”としてもエラーで終了してしまう。postgis-1.4.1/extras/template_gis/Makefileが今のバージョンの構成に対応していない。template_gisのプログラムは使われなくなったようだ。理由は分からないが。
仕方ないのPostGISのマニュアルとtemplate_gisディレクトリの中を参考にし、PostGISのSQLライブラリを使って“自前”の template_postgisデータベースを作成した。
ここで更に、PostGIS用データベースを作成する方法はPostGIS Ver 1.3 と Ver 1.4でパスやファイル名などが微妙に異なっている。以下に両方の手順を載せておくが、Ver 1.3に関してはUbuntu 9.10の標準パッケージとしてインストールしたVer 1.3.5、Ver 1.4に関してはオープンソースからインストールしたVer 1.4.1の例となる。
Ver 1.3での手順
postgres@postgis:~$ createdb template_postgis postgres@postgis:~$ createlang plpgsql template_postgis postgres@postgis:~$ psql -d template_postgis -f /usr/share/postgresql-8.3-postgis/lwpostgis.sql postgres@postgis:~$ psql -d template_postgis -f /usr/share/postgresql-8.3-postgis/spatial_ref_sys.sql
Ver 1.4での手順
postgres@postgis:~$ createdb template_postgis postgres@postgis:~$ createlang plpgsql template_postgis postgres@postgis:~$ psql -d template_postgis -f /usr/share/postgresql/8.4/contrib/postgis.sql postgres@postgis:~$ psql -d template_postgis -f /usr/share/postgresql/8.4/contrib/spatial_ref_sys.sql postgres@postgis:~$ psql -d template_postgis -f /usr/share/postgresql/8.4/contrib/postgis_comments.sql
ここでVer 1.3の手順には“postgis_comments.sql”を読み込むステップが無いが、これはUbuntu 9.10の標準パッケージからインストールするとpostgis_comments.sqlがインストールされないため。オープンソースのVer 1.3にはpostgis_comments.sqlがあるらしい(未確認)。なお、postgis_comments.sqlは読み込まなくても動作には支障はない。
作成した“template_postgis”をテンプレート・データベースとして扱えるように設定する。また、PostGISのデータベースやテーブルを扱うための“postgis”というロールを作って置き、PostGISデータベースを扱うユーザはこのロールに属せるように設定しておく。
先ず、“postgis”というロールを作る。
postgres@postgis:~$ psql psql (8.4.2) Type "help" for help. postgres=# CREATE ROLE postgis; CREATE ROLE postgres=#
この例ではpostgisはログイン属性の無い単なるロールであるが、postgisというユーザでPostgreSQLへアクセスし管理したい場合は、CREATE USERを使ってロールを作成しても構わない。
次に、データベースtemplate_postgisやその中のテーブルのオーナーをpostgisに変更する。
postgres=# ALTER DATABASE template_postgis OWNER TO postgis ; ALTER DATABASE postgres=# \c template_postgis psql (8.4.2) You are now connected to database "template_postgis". template_postgis=# \d List of relations Schema | Name | Type | Owner --------+------------------+-------+---------- public | geometry_columns | table | postgres public | spatial_ref_sys | table | postgres (2 rows) template_postgis=# ALTER TABLE geometry_columns OWNER TO postgis ; ALTER TABLE template_postgis=# ALTER TABLE spatial_ref_sys OWNER TO postgis ; ALTER TABLE template_postgis=# \d List of relations Schema | Name | Type | Owner --------+------------------+-------+--------- public | geometry_columns | table | postgis public | spatial_ref_sys | table | postgis (2 rows) template_postgis=#
最後にデータベースtemplate_postgisにテンプレート属性を設定し、更に“接続不可”属性を与える。
template_postgis=# VACUUM FULL ; VACUUM template_postgis=# VACUUM FREEZE ; VACUUM template_postgis=# \c postgres psql (8.4.2) You are now connected to database "postgres". postgres=# UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template_postgis' ; UPDATE 1 postgres=# UPDATE pg_database SET datallowconn = FALSE WHERE datname = 'template_postgis' ; UPDATE 1 postgres=# \q postgres@postgis:~$
参考までに、以上の手続きをLinuxシェルから実行する場合の手順を次に載せておく。これをシェルスクリプトにまとめればPostGISテンプレート作成プログラムが出来る。(PostgreSQL管理者アカウント(“postgres”等)で実行する。)
createdb template_postgis createlang plpgsql template_postgis psql -d template_postgis -f /usr/share/postgresql/8.4/contrib/postgis.sql psql -d template_postgis -f /usr/share/postgresql/8.4/contrib/spatial_ref_sys.sql psql -d template_postgis -f /usr/share/postgresql/8.4/contrib/postgis_comments.sql psql -c "CREATE ROLE postgis ;" psql -c "ALTER DATABASE template_postgis OWNER TO postgis ;" psql -d template_postgis -c "ALTER TABLE geometry_columns OWNER TO postgis ;" psql -d template_postgis -c "ALTER TABLE spatial_ref_sys OWNER TO postgis ;" psql -d template_postgis -c "VACUUM FULL ;" psql -d template_postgis -c "VACUUM FREEZE ;" psql -c "UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template_postgis' ;" psql -c "UPDATE pg_database SET datallowconn = FALSE WHERE datname = 'template_postgis' ;"