aptを使って、debパッケージで postgresとlibpg-perlを導入
# apt-get install postgresql
# apt-get install libpg-perl
//直接接続とtcp接続に関して、password認証になるよう設定
対象ファイル:/etc/postgresql/7.4/main/pg_hba.conf
以下の通り編集
local all all password
host all all 127.0.0.1 255.255.255.255 password
//postgresユーザになってテスト用データベース;foobardbを作成
# su - postgres
% createdb foobardb
//foobardbに接続
% psql foobardb
//テスト用テーブル;tableを作成してデータを挿入
foobardb=#create table info(id int, msg varchar(20));
foobardb=#insert into info (id,msg) values ( 1 , 'hello');
foobardb=#insert into info (id,msg) values ( 2 , 'goodbye');
//接続用ユーザを作成
foobardb=#create user testfoo with password 'foobar';
//テストユーザ(testfoo)からのアクセスを許可しておく
foobardb=# grant all privileges on info to testfoo ;
//接続確認
$ psql -U testfoo -d foobardb -W
//perlからPgを使って接続するサンプルを作成
サンプルは、atmark-ITを参考に作成
file:/usr/lib/cgi-bin/psql.pl#!/usr/bin/perl
use Pg;
$conn = Pg::connectdb("dbname=foobardb user=testfoo password=foobar");print "Content-type: text/html\n\n";
print "<html><head><title>CGI-Perl usePg TEST</title>\n\";
print "<meta http-equiv='content-type' content='text/html; charset=shift_jis\'>\n";
print "</head><body><h2>DBtest</h2>\n";$select = $conn ->exec("select * from info");
$check = $select->ntuples;if($check != 0){
print "<table border=1>";
for ($i=0;$i<$check;$i++){
$id = $select->getvalue($i,0);
$name = $select->getvalue($i,1);
print "<tr><td>$id </td><td> $name</\td></tr>\n";
}
print "</table>";
}print "$err</body></html>\n";
exit;
// cgi単体で動作確認
# chmod +x psql.pl
# chmod og+r psql.pl
# psql.pl
// ブラウザで動作確認
http://localhost/cgi-bin/psql.pl
■参考記事
PostgreSQLで作るLinuxデータベース(3)
PostgreSQLをプログラムで操作する
http://www.atmarkit.co.jp/flinux/rensai/postgres03/postgres03.html