PostgreSQL で copy to と copy from 活用の豆情報

Last update: January 5, 2008 JST

RedHatLinux 8.0 に標準でインストールされた PostgreSQL での実例です。
ある日、このサイトを訪れた方からメールを頂きました。その内容は 「PHP 処理でエラーが出てます。」との事、調べてみると PostgreSQL 用に作ったテーブルにある、文字列型の最大長(以下赤文字部分)が少ない上に、その最大長を超えた場合のエラー処理をしていない為でした。

create table access_page ( number integer, date varchar(10), time varchar(8), this_time integer, last_time integer, filename varchar(80), ip varchar(15), port integer, user_agent varchar(80), http_referer varchar(80), primary key (number) );

$_SERVER['HTTP_REFERER'] で取得できる文字数は、私の予想をはるかに越えていました。そこで上記エラーをクリアーする為、文字列型の最大長を80から、とりあえず255に変更することにしました。
255文字分でも足りないかも? そんな時は、$HTTP_REFERER = substr($_SERVER['HTTP_REFERER'], 0, 255); でエラーしない様に文字数を制限します。

さて、copy to でデータをバックアップして、この間にテーブルを作り直し copy from でデータをもとに戻します。

[akihito@kathy akihito]$ psql wwwdb
Welcome to psql, 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
wwwdb=> \z
Access privileges for database "wwwdb" Table | Access privileges -------------+-------------------------------- access_page | {=,akihito=arwdRxt,apache=arw} (1 row)
wwwdb=> \d access_page
Table "access_page" Column | Type | Modifiers --------------+-----------------------+----------- number | integer | not null date | character varying(10) | time | character varying(8) | this_time | integer | last_time | integer | filename | character varying(80) | ip | character varying(15) | port | integer | user_agent | character varying(80) | http_referer | character varying(80) | Primary key: access_page_pkey
wwwdb=> \copy access_page to /home/akihito/access_page.dat
wwwdb=> drop table access_page
wwwdb-> ;
DROP
wwwdb=> \q
[akihito@kathy akihito]$ vi access_page.sql
create table access_page ( number integer, date varchar(10), time varchar(8), this_time integer, last_time integer, filename varchar(80), ip varchar(15), port integer, user_agent varchar(255), http_referer varchar(255), primary key (number) ); grant all on access_page to akihito; grant select, insert, update on access_page to apache;
[akihito@kathy akihito]$ psql -f access_page.sql wwwdb
psql:access_page.sql:13: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'access_page_pkey' for table 'access_page' CREATE GRANT GRANT
[akihito@kathy akihito]$ psql wwwdb
Welcome to psql, 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
wwwdb=> \d access_page
Table "access_page" Column | Type | Modifiers --------------+------------------------+----------- number | integer | not null date | character varying(10) | time | character varying(8) | this_time | integer | last_time | integer | filename | character varying(80) | ip | character varying(15) | port | integer | user_agent | character varying(255) | http_referer | character varying(255) | Primary key: access_page_pkey
wwwdb=> \copy access_page from /home/akihito/access_page.dat
\.
wwwdb=> \q
[akihito@kathy akihito]$


home  ご感想は Kogure Akihito へ。 back top next