Windows web server --- PostgreSQL 8.3.x---   Mod.18Jan2009   >>TOP

データベースを作成するのにインストールしておくと便利である。PHPとの連携もよく使いやすい。

メモ:C:\WINDOWS\php.ini で extension=php_pgsql.dll を有効にしておくことが必要。
また extension=php_mbstring.dll も有効にし,[mbstring] の設定も必要(日本語を使う場合)。
ただ,PHP5.2.6 以降ではエラーで使えない?
( Fatal error: Call to undefined function pg_connect() と表示される )
PHP5.2.5で動作を確認。

PostgresSQL 8.3.x インストール

ソースの入手先: http://www.postgresql.jp/
マニュアル: PostgreSQL
postgresql-8.3.0-1.zip をダウンロードして,解凍すると以下の3つのファイルができる。
  • README.txt
  • postgresql-8.3.msi
  • postgresql-8.3-int.msi
  • SETUP.bat
  • UPGRADE.bat
インストールする前に古いバージョンが既にインストールされている場合は,まずアンインストールを行い,C:\Program Files\ にある PostgreSQLフォルダを削除する。
postgresユーザが削除されてない可能性があるのでコマンドプロンプトを開き以下のコマンドで削除しておく。
net user postgres /delete


インストール

postgresql-8.3.msi をダブルクリックしてインストールを開始する。
  1. ダウンロードした postgresql-8.3.msi をダブルクリック
  2. Japanese/JAPAN を選択し,Start (をクリック) [Fig.1]
  3. 次へ (をクリック) [Fig.2]
  4. 次へ (をクリック) [Fig.3]
  5. 次へ (をクリック) [Fig.4]
  6. 次へ (をクリック) [Fig.5]
  7. はい (をクリック) [Fig.6]
  8. OK (をクリック) [Fig.7]
  9. Encoding は各自の環境に合わせて選択し,パスワードを入力(ここでは postgres としておく)し,
    次へ (をクリック) [Fig.8]
  10. 次へ (をクリック) [Fig.9]
  11. 次へ (をクリック) [Fig.10]
  12. 次へ (をクリック) [Fig.11]
    しばらく待つ [Fig.12]
  13. スタックビルダを使って終了する のチェックを外し,終わる (をクリック) [Fig.13]

データベースの操作
  • スタート ⇒ すべてのプログラム ⇒ PostgreSQL 8.3 ⇒コマンド・プロンプト(選ぶ)
    (太字は入力部)

  • sampledbというデータベースの作成例:
    createdb -U postgres sampledb
    (Password:postgres)
    CREATE DATABASE (バグ?なのか表示されない)

  • sampledbにアクセス:
    psql -U postgres sampledb
    (Password for user postgres:postgres)
    Welcome to psql 8.3.0, the PostgreSQL interactive terminal.

    Type: \copyright for distribution terms
    \h for help with SQL commands
    \? for help with psql commands
    \g or terminate with semicolon to execute query
    \q to quit

    sampledb=#

  • sampledb内にテーブル(table1)を作成:
    sampledb=# CREATE TABLE table1(id integer primary key,
    sampledb(# name varchar(50),
    sampledb(# comment text)
    sampledb-# WITH OIDS;
    NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "table1_pkey" for
    table "table1"
    CREATE TABLE
    sampledb=#

  • sampledb内のtable1を削除:
    sampledb=# DROP TABLE table1;
    DROP TABLE
    sampledb=#
  • データベースから抜ける
    sampledb=# \q

  • sampledbというデータベースの削除例:
    dropdb -U postgres sampledb
    (Password:postgres)
    DROP DATABASE (バグ?なのか表示されない)


使用例
データベースのテーブルを作成
スタート ⇒ すべてのプログラム ⇒ PostgreSQL 8.3 ⇒コマンド・プロンプト(選ぶ)
C:\Program Files\PostgreSQL\8.3\bin>CREATEDB -U postgres sampledb
Password:postgres

C:\Program Files\PostgreSQL\8.3\bin>psql -U postgres sampledb
Password for user postgres:postgres
Welcome to psql 8.3.5, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

sampledb=# CREATE TABLE diary(
sampledb(# sn serial unique not null,
sampledb(# godate text,
sampledb(# doc text,
sampledb(# num integer)
sampledb-# WITHOUT OIDS;
NOTICE: CREATE TABLE will create implicit sequence "diary_sn_seq" for serial co
lumn "diary.sn"
NOTICE: CREATE TABLE / UNIQUE will create implicit index "diary_sn_key" for tab
le "diary"
CREATE TABLE
sampledb=# grant all on diary to public;
GRANT
sampledb=# grant all on diary_sn_seq to public;
GRANT
sampledb=# \q

C:\Program Files\PostgreSQL\8.3\bin>

ブラウザーのフォームからデータをUP
ソース: input.php 表示例
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=EUC-JP">
<TITLE></TITLE>
</HEAD>
<BODY>
<FORM action="add.php" method="post">
<DIV align="center">
<H2>データの追加</H2>
<TABLE border="0" width="500">
<TBODY>
<TR>
<TD width="145" align="right">日 付:</TD>
<TD width="345"><INPUT size="6" type="text" name="y">年 <INPUT size="4" type="text" name="m">月 <INPUT size="4" type="text" name="d">日</TD>
</TR>
<TR>
<TD width="145" align="right">内 容:</TD>
<TD width="345"><TEXTAREA rows="2" cols="40" name="doc"></TEXTAREA></TD>
</TR>
<TR>
<TD align="right">釣 果:</TD>
<TD><INPUT size="5" type="text" name="num">尾</TD>
</TR>
<TR>
<TD width="145"></TD>
<TD width="345"><BR>
<INPUT type="submit" value="データの追加"></TD>
</TR>
</TBODY>
</TABLE>
</DIV>
</FORM>
</BODY>
</HTML>

フォームから送られたデータをデータベースに書き込む
ソース: add.php
<?

$year = trim($_POST[y]); $year = htmlentities($year,1,'euc-jp');
$month = trim($_POST[m]); $month = htmlentities($month,1,'euc-jp');
$day = trim($_POST[d]); $day = htmlentities($day,1,'euc-jp');
$year = mb_convert_kana($year,"a","euc-jp");//全角->半角
$month = mb_convert_kana($month,"a","euc-jp");//全角->半角
$day = mb_convert_kana($day,"a","euc-jp");//全角->半角
$year = sprintf("%04d",$year);
$month = sprintf("%02d",$month);
$day = sprintf("%02d",$day);
$doc = trim($_POST[doc]); $doc = htmlentities($doc,1,'euc-jp');
$num = trim($_POST[num]); $num = htmlentities($num,1,'euc-jp');
$num = mb_convert_kana($num,"a","euc-jp");//全角->半角

if($year == NULL && $month == NULL && $day == NULL){header("Location: ./input.php");exit;}
if($doc == NULL){header("Location: ./input.php");exit;}
if($num == NULL){$num = 0;}

$godate = $year."-".$month."-".$day;

$con = pg_connect("dbname=sampledb user=postgres password=postgres");
$in="INSERT INTO diary (godate,doc,num) VALUES ('$godate','$doc','$num');";
$pg = pg_query($con,"$in");
pg_close($con);

echo "Data have been saved."

?>

データベースに保存されているデータを表示
ソース: disp.php 表示例
<?
$con = pg_connect("dbname=sampledb user=postgres password=postgres");
$rs = pg_query($con, "SELECT * FROM diary order by sn;");
$maxrows = pg_num_rows($rs);
?>
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=EUC-JP">
<TITLE></TITLE>
</HEAD>
<BODY>
<P>データベース表示例<BR>
<BR>
</P>
<TABLE border="1" width="500">
<TBODY>
<TR>
<TD width="100" align="center">釣行日</TD>
<TD align="center">内 容</TD>
<TD width="50" align="center">釣果</TD>
</TR>
<?
for ($i = 0; $i < $maxrows; $i++) {

$row = pg_fetch_row($rs, $i);
$sn = $row[0];
$godate = $row[1];
$doc = $row[2];
$num = $row[3];
$doc = mb_convert_encoding($doc,"EUC-JP","auto");
$doc = ereg_replace("\r\n", "<BR>", $doc);
$doc = ereg_replace("\r", "<BR>", $doc);
$doc = ereg_replace("\n", "<BR>", $doc);

echo "<TR>\n";
echo "<TD align=\"center\">".$godate."</TD>\n";
echo "<TD>".$doc."</TD>\n";
echo "<TD align=\"center\">".$num."</TD>\n";
echo "</TR>\n";
}
?>
</TBODY>
</TABLE>
</BODY>
</HTML>


編集,削除は次回・・・