各種情報とソースの入手
-
開発者によるWebサイト
http://www.postgresql.org/ ・・・ PostgreSQL
http://gborg.postgresql.org/project/pgperl/projdisplay.php ・・・ pgperl
http://gborg.postgresql.org/project/dbdpg/projdisplay.php ・・・ DBD::Pg
http://dbi.perl.org/ ・・・ DBI -
日本語による情報
http://www.postgresql.jp/ ・・・ 日本 PostgreSQL ユーザ会
http://osb.sra.co.jp/PostgreSQL/index.php ・・・ SRAによる PostgreSQL 情報 -
PostgreSQL の最新リリース (tar archive のサイズ: 11,265,028 byte)
ftp://ftp.sra.co.jp/pub/cmd/postgres/7.3.4/postgresql-7.3.4.tar.gz ・・・ SRAのftpサイト
ftp://ftp.t.ring.gr.jp/pub/misc/db/postgresql-jp/7.3.4/postgresql-7.3.4.tar.gz
※ 配付元である ftp.postgresql.org は混雑により繋がらないようです。
※ 日本のオフィシャルミラーサイト(ftp.jaist.ac.jp)にも最新版は無いようです。 -
pgperl の最新リリース (tar archive のサイズ: 25,346 byte)
ftp://gborg.postgresql.org/pub/pgperl/stable/pgperl-2.0.2.tar.gz ・・・ 配付元 -
DBD::Pg の最新リリース (tar archive のサイズ: 80,896 byte) ・・・ バグあり!
ftp://gborg.postgresql.org/pub/dbdpg/stable/DBD-Pg-1.22.tar.gz ・・・ 配付元
http://search.cpan.org/CPAN/authors/id/D/DW/DWHEELER/DBD-Pg-1.22.tar.gz -
DBD::Pg の最新βテスト版 (tar archive のサイズ: 82,694 byte)
http://www.remotelinux.com/rlippan/DBD-Pg-1.31_2.tar.gz
※ この URL は Dbdpg-general メーリングリスト でアナウンスされたものです。 頻繁に版が更新されているようなので、 最新の情報を確認してからダウンロードしたほうが良いと思われます。 -
DBI の最新リリース (tar archive のサイズ: 294,495 byte)
http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/DBI-1.37.tar.gz ・・・ 配付元 -
Apache::DBI の最新リリース (tar archive のサイズ: 26,899 byte)
http://search.cpan.org/CPAN/authors/id/A/AB/ABH/Apache-DBI-0.92.tar.gz ・・・ 配付元
準備
-
グループID "postgres" を登録します。
/etc/group ファイルに以下のような設定を追加します。postgres:*:133:www
-
ユーザID "postgres" を登録します。
vipw コマンドで以下のような設定を追加します。postgres:*:133:133::0:0:PostgreSQL:/var/pgsql:/bin/csh
PostgreSQL のコンパイル、インストール
-
ソースを展開し、configure を実行し、make します。
なお、PostgreSQL の make には GNU make を使用します。% tar xzvf …/postgresql-7.3.4.tar.gz % cd postgresql-7.3.4 % ./configure --without-readline % gmake
configure オプションの --without-readline は readline ライブラリを使用しない指定です。 OpenBSD に標準添付の readline ライブラリ使用すると、 psql コマンド等で日本語が通らなくなります。 行エディット機能やヒストリ機能の利便性を考えると、 日本語対応の readline ライブラリを導入したほうが良いかとも思いましたが、 emacs のバッファ内で使う(M-x sql-postgres)のであれば必要ないので、 今回は readline ライブラリを使わないことにしました。 -
添付のテストプログラムで動作確認をします。
なお、テストは複数のプロセスで並行して行われるので、 limit コマンドで実行可能プロセス数の上限を上げてから実行する必要があります。% limit maxproc 120 % gmake check ・・・ テスト準備が行われる ・・・ ============== removing existing temp installation ============== ============== creating temporary installation ============== ============== initializing database system ============== ============== starting postmaster ============== running on port 65432 with pid 1234 ============== creating database "regression" ============== CREATE DATABASE ALTER DATABASE ============== dropping regression test user accounts ============== ============== installing PL/pgSQL ============== ============== running regression test queries ============== parallel group (13 tests): text oid int4 varchar char float4 int2 name boolean int8 float8 bit numeric boolean ... ok char ... ok name ... ok varchar ... ok text ... ok int2 ... ok int4 ... ok int8 ... ok oid ... ok float4 ... ok float8 ... ok bit ... ok numeric ... ok test strings ... ok test numerology ... ok parallel group (20 tests): comments lseg point time tinterval polygon box timet z path circle interval date timestamp reltime timestamptz inet abstime type_sani ty oidjoins opr_sanity point ... ok lseg ... ok box ... ok path ... ok polygon ... ok circle ... ok date ... ok time ... ok timetz ... ok timestamp ... ok timestamptz ... ok interval ... ok abstime ... ok reltime ... ok tinterval ... ok inet ... ok comments ... ok oidjoins ... ok type_sanity ... ok opr_sanity ... ok test geometry ... ok test horology ... ok test insert ... ok test create_function_1 ... ok test create_type ... ok test create_table ... ok test create_function_2 ... ok test copy ... ok parallel group (7 tests): create_aggregate create_operator vacuum inherit trigg ers constraints create_misc constraints ... ok triggers ... ok create_misc ... ok create_aggregate ... ok create_operator ... ok inherit ... ok vacuum ... ok parallel group (2 tests): create_view create_index create_index ... ok create_view ... ok test sanity_check ... ok test errors ... ok test select ... ok parallel group (16 tests): select_distinct_on select_distinct portals select_in to transactions case random union hash_index arrays subselect select_having aggr egates select_implicit join btree_index select_into ... ok select_distinct ... ok select_distinct_on ... ok select_implicit ... ok select_having ... ok subselect ... ok union ... ok case ... ok join ... ok aggregates ... ok transactions ... ok random ... failed (ignored) portals ... ok arrays ... ok btree_index ... ok hash_index ... ok test privileges ... ok test misc ... ok parallel group (5 tests): portals_p2 select_views cluster rules foreign_key select_views ... ok portals_p2 ... ok rules ... ok foreign_key ... ok cluster ... ok parallel group (11 tests): truncate temp prepare without_oid conversion copy2 l imit domain rangefuncs plpgsql alter_table limit ... ok plpgsql ... ok copy2 ... ok temp ... ok domain ... ok rangefuncs ... ok prepare ... ok without_oid ... ok conversion ... ok truncate ... ok alter_table ... ok ============== shutting down postmaster ============== ================================================== 88 of 89 tests passed, 1 failed test(s) ignored. ================================================== ・・・ %random のテストが fail になっていますが、実行結果を固定のテキストと比較している関係で、 結果が一定しない乱数のテストでは正常に動作していても fail になる場合があるそうです。 -
インストールします。
% su # gmake install
-
PostgreSQL のシェアードライブラリに標準で(LD_LIBRARY_PATH の設定無しに)アクセスできるように、
/etc/rc.conf の shlib_dirs の設定を以下のように変更します。
shlib_dirs=/usr/local/pgsql/lib # extra directories for ldconfig
上の設定はOSを立ち上げた時に読み込まれるものなので、 この後の作業のために、手動で以下のコマンドを実行し、同等の設定を行います。# ldconfig /usr/X11R6/lib /usr/local/lib /usr/local/pgsql/lib
-
データベースを置くディレクトリを作成します。
# mkdir /var/pgsql # chown postgres:postgres /var/pgsql # chmod 750
-
データベース管理者 postgres の環境設定をします。
以下のような内容で /var/pgsql/.cshrc を作成します。set pgbase=/usr/local/pgsql set path=($path $pgbase/bin) set prompt='postgres% ' setenv PGDATA /var/pgsql/data setenv PGHOST /var/pgsql setenv MANPATH /usr/share/man:$pgbase/man
-
データベースクラスタを作成します。
ここからは、データベース管理者 postgres に su して作業を行います。
日本語を使う場合、initdb には --no-locale オプションが必須です。# su - postgres postgres% initdb --no-locale --encoding=EUC_JP 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. creating directory /var/pgsql/data... ok creating directory /var/pgsql/data/base... ok creating directory /var/pgsql/data/global... ok creating directory /var/pgsql/data/pg_xlog... ok creating directory /var/pgsql/data/pg_clog... ok creating template1 database in /var/pgsql/data/base/1... ok creating configuration files... ok initializing pg_shadow... ok enabling unlimited row size for system tables... ok initializing pg_depend... ok creating system views... ok loading pg_description... ok creating conversions... ok setting privileges on built-in objects... ok vacuuming database template1... ok copying template1 to template0... ok Success. You can now start the database server using: /usr/local/pgsql/bin/postmaster -D /var/pgsql/data or /usr/local/pgsql/bin/pg_ctl -D /var/pgsql/data -l logfile start postgres% -
PostgreSQLのサーバを起動し、管理者DBを作成し、管理者のパスワードを設定します。
また、一般ユーザ(この例ではWebからのアクセスを想定した www)を登録します。postgres% pg_ctl start -l /var/pgsql/data/postmaster.log -o '-k /var/pgsql' postmaster successfully started postgres% createdb --encoding=EUC_JP CREATE DATABASE postgres% psql Welcome to psql 7.3.4, the PostgreSQL interactive terminal. Type: \copyright for distribution terms \h for help with SQL commands \? for help on internal slash commands \g or terminate with semicolon to execute query \q to quit postgres=# ALTER USER postgres WITH PASSWORD '********'; ALTER USER postgres=# CREATE USER www; CREATE USER postgres=# CREATE DATABASE www WITH ENCODING 'EUC_JP'; CREATE DATABASE postgres=# \du List of database users User name | User ID | Attributes -----------+---------+---------------------------- postgres | 1 | superuser, create database www | 100 | (2 rows) postgres=# \l List of databases Name | Owner | Encoding -----------+----------+---------- postgres | postgres | EUC_JP template0 | postgres | EUC_JP template1 | postgres | EUC_JP www | postgres | EUC_JP (4 rows) postgres=# \q postgres% -
認証の設定ファイルを更新し、動作を確認します。
この例では、ネットワーク経由の接続は許可せず、 UNIXドメインソケットによる接続のみを許可する設定です。 そして、UNIXドメインソケットは postgres グループのみがアクセス可能なパスに置くことで、 利用者を制限します。 また、Webサーバのスクリプトからのアクセスを想定し、 データベース www にデータベースユーザ www で接続する場合だけパスワード不要とし、 他の場合はデータベース上の暗号化パスワードで認証する設定になっています。postgres% vi /var/pgsql/data/pg_hba.conf ------------------------- 以下のような内容に更新 ------------------------- # TYPE DATABASE USER IP-ADDRESS IP-MASK METHOD local www www trust local all all md5 -------------------------------------------------------------------------- postgres% pg_ctl reload postmaster successfully signaled postgres% postgres% psql Password: ← パスワードを求められます。 Welcome to psql 7.3.4, the PostgreSQL interactive terminal. Type: \copyright for distribution terms \h for help with SQL commands \? for help on internal slash commands \g or terminate with semicolon to execute query \q to quit postgres=# \q postgres% psql -U www Welcome to psql 7.3.4, the PostgreSQL interactive terminal. ← パスワード無しでログインできます。 Type: \copyright for distribution terms \h for help with SQL commands \? for help on internal slash commands \g or terminate with semicolon to execute query \q to quit www=# \q postgres%
pgperl のコンパイル、インストール
pgperl は 7.2.X 版までは PostgreSQL 本体に添付される形で配付されていた Perl インタフェースモジュールで、モジュール名は Pg です。 現在は、GBorg 上のプロジェクトとして独立して配付しています。 独自仕様のインタフェースで、 Apache + mod_perl の環境における持続的接続機能は提供されていません。
-
ソースを展開し、configure を実行し、make します。
% tar xzvf …/pgperl-2.0.2.tar.gz % cd Pg-2.0.2/ % setenv POSTGRES_HOME /usr/local/pgsql % perl Makefile.PL % make
-
添付のテストプログラムで動作確認をします。
なお、テストはデータベースとデータベースユーザを登録できるユーザが行う必要があります。 また、そのユーザはパスワード無しでアクセスできなければなりません。 以下の実行例は、 ユーザ postgres を一時的にパスワード無しでアクセス可能な設定にして実行したものです。postgres% make test PERL_DL_NONLAZY=1 /usr/bin/perl "-Iblib/lib" "-Iblib/arch" test.pl Pg::conndefaults ........ ok Pg::connectdb ........... ok $conn->exec ............. ok $conn->errorMessage ..... ok $conn->db ............... ok $conn->user ............. ok $conn->port ............. ok $conn->cmdStatus ........ ok $conn->oidStatus ........ ok $conn->getline .......... ok $conn->endcopy .......... ok $result->cmdTuples ...... ok $result->fname .......... ok $result->ftype .......... ok $result->fsize .......... ok $result->fnumber ........ ok $result->fetchrow ....... ok test sequence finished. postgres%
-
インストールします。
% su # make install
DBI のコンパイル、インストール
DBI は Perl からデータベースをアクセスするための共通インタフェースモジュールです。 このモジュールを通すことにより、PostgreSQL や Oracle など、 多くのデータベースを共通のインターフェース仕様で使うことができます。
-
ソースを展開し、configure を実行し、make します。
% tar xzvf …/DBI-1.37.tar.gz % cd DBI-1.37/ % perl Makefile.PL % make
-
添付のテストプログラムで動作確認をします。
% make test PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t t/01basics.............ok t/02dbidrv.............ok t/03handle.............ok t/04mods...............ok t/05thrclone...........skipped all skipped: this perl 5.008 not configured to support iThreads t/06attrs..............ok t/07kids...............ok t/08keeperr............ok t/10examp..............ok t/15array..............ok t/20meta...............ok t/30subclass...........ok t/40profile............ok t/41prof_dump..........ok t/42prof_data..........ok t/60preparse...........ok t/80proxy..............skipped all skipped: modules required for proxy are probably not installed (e.g. , RPC/PlClient.pm) t/zz_01basics_pp....... Using DBI::PurePerl (DBI_PUREPERL=2) t/zz_01basics_pp.......ok t/zz_02dbidrv_pp.......ok t/zz_03handle_pp.......ok t/zz_04mods_pp.........ok t/zz_05thrclone_pp.....skipped all skipped: this perl 5.008 not configured to support iThreads t/zz_06attrs_pp........ok t/zz_07kids_pp.........skipped all skipped: $h->{Kids} attribute not supported for DBI::PurePerl t/zz_08keeperr_pp......ok t/zz_10examp_pp........ok 1/245 Taint mode switching tests skipped t/zz_10examp_pp........ok t/zz_15array_pp........ok t/zz_20meta_pp.........ok t/zz_30subclass_pp.....ok t/zz_40profile_pp......skipped all skipped: profiling not supported for DBI::PurePerl t/zz_41prof_dump_pp....skipped all skipped: profiling not supported for DBI::PurePerl t/zz_42prof_data_pp....skipped all skipped: profiling not supported for DBI::PurePerl t/zz_60preparse_pp.....skipped all skipped: preparse not supported for DBI::PurePerl t/zz_80proxy_pp........skipped all skipped: modules required for proxy are probably not installed (e.g. , RPC/PlClient.pm) All tests successful, 9 tests skipped. Files=34, Tests=1203, 16 wallclock secs (13.21 cusr + 1.45 csys = 14.66 CPU) PERL_DL_NONLAZY=1 /usr/bin/perl "-Iblib/lib" "-Iblib/arch" test.pl test.pl DBI test application $Revision: 1.10 $ Switch: DBI 1.37 by Tim Bunce, 1.37 Available Drivers: ExampleP, Proxy dbi:ExampleP:: testing 5 sets of 20 connections: Connecting... 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Disconnecting... Connecting... 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Disconnecting... Connecting... 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Disconnecting... Connecting... 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Disconnecting... Connecting... 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Disconnecting... Made 100 connections in 0 wallclock secs ( 0.10 usr + 0.01 sys = 0.11 CPU) Testing handle creation speed... 10000 NullP statement handles cycled in 1.8 cpu+sys seconds (5464 per sec) test.pl done % -
インストールします。
% su # make install
DBD::Pg のコンパイル、インストール
DBD::Pg は DBI の配下で PostgresSQL を使うためのデータベースドライバモジュールです。
-
ソースを展開し、configure を実行し、make します。
% tar xzvf …/DBD-Pg-1.31_2.tar.gz % cd DBD-Pg-1.31_2/ % perl Makefile.PL % make
-
添付のテストプログラムで動作確認をします。
% setenv DBI_DSN DBI:Pg:dbname=www % setenv DBI_USER www % setenv DBI_PASS % make test PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t t/00basic...........ok t/01connect.........ok t/01constants.......ok t/01setup...........ok t/02prepare.........ok t/03bind............ok t/04execute.........ok t/05fetch...........ok t/06disconnect......ok t/07reuse...........ok t/08txn.............ok t/09autocommit......ok t/10chopblanks......ok t/11quoting.........ok t/12placeholders....ok t/13pgtype..........ok t/15funct...........ok t/99cleanup.........ok All tests successful. Files=18, Tests=209, 6 wallclock secs ( 4.18 cusr + 0.46 csys = 4.64 CPU) %
-
インストールします。
% su # make install
Apache::DBI のコンパイル、インストール
Apache::DBI は Apache + mod_perl の環境で、 DBI によるデータベースの持続的接続機能を提供するモジュールです。 このモジュールを事前にロードしておくだけで、 プログラムには手を加えずに持続的接続機能を実現することができます。
-
ソースを展開し、configure を実行し、make します。
% tar xzvf …/Apache-DBI-0.92.tar.gz % cd Apache-DBI-0.92/ % perl Makefile.PL % make
-
インストールします。
% su # make install
Apache の環境設定
Apache + mod_perl の環境で、 Apache::DBI + DBI + DBD::Pg を使って PostgreSQL にアクセスするには、 Apache の設定に追加が必要になります。
-
Apache 起動時に Perl の初期化プログラム startup.pl を実行する設定をします。
以下のような設定を httpd.conf の <IfModule mod_perl.c> セクションの中に追加します。PerlRequire "/usr/local/apache/conf/startup.pl"
-
以下のような内容で /usr/local/apache/conf/startup.pl を作成します。
#!/usr/bin/perl use Apache::DBI; use DBI; use DBD::Pg; Apache::DBI->connect_on_init('DBI:Pg:host=/var/pgsql;dbname=www','www','', {AutoCommit=>0, RaiseError=>1}); -
以上の設定を反映させるためには Apache を立ち上げ直します。
restartでは Perl の初期化が行われないようなので、 一旦停止してから起動し直します。# /usr/local/apache/bin/apachectl stop # /usr/local/apache/bin/apachectl sslstart
-
サンプルプログラムです。
簡単な SELECT を実行して、結果を表示します。
mod_perl の Apache::Run 配下で実行する想定で作ったものですが、 インタフェース仕様が CGI-BIN とコンパチなので、 通常の CGI-BIN 環境でも、このままで動作します。#!/usr/bin/perl -w use DBI; my $dbh = DBI->connect('DBI:Pg:host=/var/pgsql;dbname=www','www','', {AutoCommit=>0, RaiseError=>1}) or die('Connect error: '.$DBI::errstr); my $sth = $dbh->prepare('select * from pg_user'); $sth->execute(); my $text = ''; my $count = 0; while(my $row = $sth->fetchrow_arrayref()){ for(my $i = 0; $i <= $#$row; $i++){ $text .= ',' if $i > 0; $text .= $$row[$i] if defined($$row[$i]); } $text .= "\n"; $count++; } $sth->finish(); $dbh->disconnect(); print("Content-type: text/plain; charset=EUC-JP\n\n"); print("#rows = $count\n"); print($text);