20080222 turbolinux11server で PostgreSQL

Linuxどっぷりな毎日が過ぎていく・・・

DBサーバー立てるのに1時間以内でインストールして運用できてしまう。
RedHat,CentOSではGUIが進んでるけど、そんな機能はWindowsに任せておけばいいじゃん。
(RedHat,CentOSなどを否定する訳ではありません。必要に応じ使用していますし、ですがGUI寄りだとPostgreSQL使いたいだけなのに不必要なサービスに気をくばったり、やたら保守運用に手間が掛かりすぎます。メーカーやベンダはそのほうが好都合?なのでしょうけど、無精もありMS-DOS程度の操作性、TOMOYOも標準ですし充分強力です。簡単に使えるようにの思考方向=GUI整備でない、ここに turbolinux11server の価値があります。)

使いたいのはセキュアな turbolinux11server に守られた PostgeSQL !
簡単便利な turbolinux11server やめられません。

一刻でも速く仕事を終わらせたいだけだったりする。
(何日も設定作業したくないですからね。)

出先でチョット見るための私的メモです。
都合上、省いたところがありますが、よろしかったら参考にしてください。

●準備

本体:富士通.Primergy.RX100.C2GHz.256MB
オンボードPromiseRAIDは、turbolinux11serverでサポート外なのでBIOSで切り離し、3wareにする。
Power Management Mode [Enabled] (ここが[Disabled]だとshutdown -h now で電源が落ちない。)

RAIDカード:3ware.9500S-4LP.JKIT(電源をかさ上げし装着)
起動時設定で、SATA.500Gbyte(Seagate.ST3500630NS)x2をRAID1化しておく。

UPS.APC社,Smart-UPS500

turbolinux11server.32bit
turbolinux10server.CD
turbolinux6.5server.CD

●インストール

電源投入直後、CD1を入れてインストール

Japanese(ja_JO.UTF-8)
同意する
標準インストール
日本語106/109キーボード
PS/2マウス

手動パーティション設定(自動パーティション設定だとRAIDしない。)
パーティション追加

/dev/sda1,Linux ext3,/boot,64MB
/dev/sda2,Linux ext3,/,476000MB
/dev/sda,Linux スワップ,swap,760MB

フォーマットするパーティション
x./dev/md1./
x./dev/md0./boot
x.フォーマット中に不良ブロックをチェックする。

ブートローダ設定(デフォルトのまま)
注:RAIDではGRUBを使用できない。

eth0 の設定
DHCPを使用しないので、
□動的にIPアドレスを割り当てるのxを消す。
起動時に有効にするはxにする。

IP:WanのIPアドレスを入力   
Mask:Wanのネットマスク
Network:Wanのネットワーク
Broadcast:Wanのブロードキャスト

eth1 の設定
DHCPを使用しないので、
□動的にIPアドレスを割り当てるのxを消す。
起動時に有効にするはxにする。

IP:LanのIPアドレスを入力   
Mask:Lanのネットマスク
Network:Lanのネットワーク
Broadcast:Lanのブロードキャスト

ホスト名:???.???.com(取得しているドメイン付きでサーバーの名称を記述)
ゲートウェイ:Wan側に設置されているゲートウェイサーバーのIP
1番目のネームサーバー(IP):Wan側に設置されているDNSサーバーのIP(プロパイダのDNSipでOK)
2番目のネームサーバー(IP):Wan側に設置されているDNSサーバーの予備IP(省略可)
3番目のネームサーバー(IP):Wan側に設置されているDNSサーバーの予備IP(省略可)

DNSのIPは入力中に自動で記入されるが使えないIPなので修正すること。

時間帯の選択で
設置場所の項目はデフォルトのまま
タイムサーバーの項目は、
タイムサーバーを使用するに×
チェック間隔は毎日
タイムサーバを入力***.***.***.jp

アカウントはrootだけ作成、ユーザーは未作成。

インストールタイプ
標準サーバー(849MB)でインストールするが、
標準サーバー(849MB)ではPostgreSQLがインストールされないので
カスタム選択にあるグループ
PostgreSQLデータベース1/10になっているのでクリックし直し
PostgreSQLデータベース10/10にする。

(全パッケージ(946MB)でも特に問題ないが、ここではPostgreSQLを使用するのが目的なので余分なものをインストールしない。)

選択後、[次] をクリック。
 
X Window System をCRTに合わせて設定

ATI Technologies Inc Rage XL 8128KByte

・・・インストール・・・(約12分)

mongoose-base-2.0-2007102619.i686.rpm
上記だけではturboserviceなど使えないので
不足分を下記の如く追加

x.追加パッケージをインストールする。
CD1が自動でイジェクトされます。
19インチラックでは手前の扉とコンピュータの間が狭い場合があります。
この場合CD装置が壊れないようにラック扉を開けておく。
CD2挿入して次へ
mongoose-2.0-2007102619.i686.rpmと X Window 関係全パッケージが無いと不便なので
□Turbo toolsに×
□X Window システム(X Window System)に×(X Window使わないなら不要)
□XFCE ウィンドウマネージャ(XFCE window manager)に×(X Window使わないなら不要)
□X Window System 開発ライブラリ(X Window System Development)に×(X Window使わないなら不要)
□X Window アプリケーション(X Window application)に×(X Window使わないなら不要)
□日本語 X Window アプリケーション(X Window apps Japanese)に×(X Window使わないなら不要)
□互換性パッケージ(Compatibility packages)に×(UPSインストール時必要)
依存関係があるものに自動で×が入るので注意、丁寧に操作。

(全部追加インストールしても問題ない。)

・・・追加インストール・・・(約6分)

セキュリティの設定は高レベル。

●起動後 eth0,eth1 の確認

# cat /etc/modules.conf
options usb-storage delay_use=0
alias eth0 e1000
alias eth1 e100
alias scsi_hostadapter pata_serverworks
alias scsi_hostadapter1 3w-9xxx
alias parport_lowlevel parport_pc
probeall block-major-117 md_linear md_raid0 md_raid1 md_raid5 lvm_vge ldev_mgr dos_part

# ifconfig

eth0      Link encap:Ethernet  HWaddr ??:??:??:??:??:??
          inet addr:Wanに使うIP.......

eth1      Link encap:Ethernet  HWaddr ??:??:??:??:??:??
          inet addr:Lanに使うIP.......

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

●ライセンス登録しないと追加パッケージ(turbo+ --cui)できない。

(全パッケージインストールしておけば追加パッケージの必要がないので
 ライセンス登録しなくとも問題なくturbolinux11serverが使えるが、
 自動最新アップデートはされない。
 アップデートで運用不具合が発生し泣かされたことが多々あるので、
 無駄なアップデートはせずに、このほうが良いかもしれない。
 こうなると使ったことはないが評価版でもいいような気もする。)

# ping www.yahoo.co.jp
(止めるのはctrl+c)

上記とかpingが通るか確認後ライセンス登録。

# turbolicensecfg
CDケースにあるIDとメールアドレスを入れて、
[Send]

turbolinuxコンピュータでメール取得済みであれば
musasabi_license.txtをコピーしたライセンスフロッピディスクを作っておく。
# fdformat /dev/fd0H1440
# mkfs /dev/fd0
# mount /dev/fd0 /mnt/floppy
# cp musasabi_license.txt /mnt/floppy
# umount /mnt/floppy

インストール中のコンピュータに挿入して
# mount /dev/fd0 /mnt/floppy
# turbolicensecfg
[Apply]ボタンをクリック
/mnt/floppy/musasabi_license.txt
[OK]
# umount /mnt/floppy

●telnetを使えるように

(セキュリティ上好ましくないので、使わないときは止めることを守れば、とても便利)

turbolinux11serverにtelnet.serverはパッケージされていない。
turbolinux10server.CD2に、
telnet-server-0.17-24.i586.rpmが入っている。
(最新のダウンロードしたものが好ましいが、手っ取り早い。)

# turbo+ --cui

[Install]
CD-ROM
F4(telnet)でサーチ(クライアントが先に見つかる)
もう一度F4でサーチ
[ ] telnet-setver.36KB でスペースキーを押し
[*] telnet-setver.36KB
Install
telnet-server-0.17.24
Continue

CDを出す。

TurboLinux11Serverはinetdでなくxinetdを使用している。

# vi /etc/xinetd.d/telnet

service telnet {
  disable   = no <-yesをnoにする
  socket_type = stream
  protocol   = tcp
  wait    = no
  user    = root
  server   = /usr/sbin/in.telnetd
  server_args = -h
}

telnet接続できるIPを1個だけ登録

# vi /etc/hosts.allow

#in.telnetd: sample.foobar.edu
の下に
in.telnetd: ???.???.???.??? : allow

インストールで自動起動設定がされてしまうので注意、
# turboservice
[Configure default services]
上記で起動最初は[*]になってます。

# turboservice
でtelnetを起動して他端末からTelnet接続して使えます。

Telnet接続用にユーザーを追加する。

# adduser dareka
# passwd dareka

Windowsパソコンtelnet.TeraTermなどから接続してみてください。

●ここまでの作業でディスク容量は?

# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda2            479771636   2129608 453271024   1% /
/dev/sda1                62193     13838     45144  24% /boot
none                    127924         0    127924   0% /dev/shm

●不足項目の入力

# turbonetcfg
Basic Settingで
取得しているドメインネームを登録   

non を ???.com

●ログ転送用にSMTP設定

システムログを管理者メールに転送するためSMTPをセットします。

# vi /etc/postfix/aliases

# Sample aliases file. Install in the location as specified by the
# output from the command "postconf alias_maps". Typical path names
# are /etc/aliases or /etc/mail/aliases.
#
#       >>>>>>>>>>      The program "newaliases" must be run after
#       >> NOTE >>      this file is updated for any changes to
#       >>>>>>>>>>      show through to Postfix.
#

# Person who should get root's mail. Don't receive mail as root!
#root:          you

# Basic system aliases -- these MUST be present
MAILER-DAEMON:  postmaster
postmaster:     root

# General redirections for pseudo accounts
bin:            root
daemon:         root
named:          root
nobody:         root
uucp:           root
www:            root
ftp-bugs:       root
postfix:        root

# Put your local aliases here.

# Well-known aliases
manager:        root
dumper:         root
operator:       root
abuse:          postmaster

# trap decode to catch security attacks
decode:         root
root:           (送りたい先のメールアドレス)ココに1行だけ挿入

そして変更を反映させます。

postalias コマンドで、/etc/postfix/aliases.db を作成します。

# postalias /etc/postfix/aliases

これですぐに設定が反映され、 root 宛てのメールが自動転送されるようになりました。もしくは、

# newaliases

と入力して、aliases.db を初期化しても設定が反映されます。

設定がすぐに反映されない場合は、Postfix を再起動、

# service postfix reload  ( または # postfix reload ) 

これで再起動します。
# /etc/init.d/postfix start
停止は
# /etc/init.d/postfix stop

自動起動の設定
# chkconfig postfix on

変更の反映
# /etc/init.d/postfix restart
または
# reboot (上記restart不要)
して確認

●APC社,Smart-UPS500

UPSを接続するたシャットダウン後、
APC社のSmart-UPS 500に電源を差し、付属のシリアルケーブルでCOM1に接続
電源を入れる。

APC社UPS用に使うユーザーを追加する。

# adduser pwrchute
# passwd pwrchute

turbolinux11serverにUPSソフトはパッケージされていない。
turbolinux6.5server.CD2に、
PowerChute plus v.4.5.2が入っている。
(最新のダウンロードしたものが好ましいが、手っ取り早い。)

# mount -t iso9660 /dev/cdrom /mnt/cdrom

(アンマウントは umount /mnt/cdrom)

# cd /mnt/cdrom/UPS/APC

# ./INSTALL
-----------------------------------------------------------------------
         PowerChute Plus for Unix v4.5.2 Installation Script
             Copyright American Power Conversion 1999
-----------------------------------------------------------------------
If you quit this script at any time before committing your choices,
the installation will not take place, and no modifications will be made
to your currently installed PowerChute products

                        1) CD-ROM
                        2) Floppy
                        3) Tape

Select the media type from which you will install: [?] 1 ← 1を入力

Enter path to mounted CD ROM [/mnt/cdrom/UPS/APC] ← リターンのみ

Would you like to see an overview of the Installation? [y/n,q]n ← y,nどちらでも良い

----------------------------------------------------------------------------
The User Interface module of this product is capable of monitoring any system
on your network running the Daemon module of PowerChute Plus for Unix.
For this reason, you may feel it is only appropriate to install the entire
distribution on certain systems.
----------------------------------------------------------------------------
NOTE: In order to perform any sort of UPS monitoring, including shutdown due
to power failure, you must install at least the Daemon module of the product!
----------------------------------------------------------------------------

                1) User Interface Module Only
                2) Daemon Module Required for UPS Monitoring Only
                3) Both the User Interface and Daemon Modules

Which Parts of PowerChute Plus for Unix do you wish to install? [?]3 ← 3を入力

                1) Matrix-UPS
                2) Smart-UPS
                3) Back-UPS
                4) Back-UPS Pro
                5) Symmetra Power Array
                6) Smart-UPS DP
 
Which APC Hardware will PowerChute Plus for Unix be running with [?]2 ← UPS製品
----------------------------------------------------------------------------
The Measure-UPS is a device which is designed to perform environmental
monitoring in conjunction with PowerChute Plus for Unix
----------------------------------------------------------------------------

Do you currently have a Measure-UPS attached to the UPS? [y/n,q] n ← 他のUNIXと連携監視しない

                1)  Solaris 2.X for SPARC
                2)  SunOS 4.X (Solaris 1.X) for SPARC
                3)  Solaris 2.X for Intel
                4)  AIX for IBM RS6000
                5)  HPUX 10.X/11.X  (700/800 series)
                6)  SCO Unix/Unixware 7.X
                7)  NCR (AT&T UNIX)
                8)  SGI Irix
                9)  UnixWare 2.X
                10) Olivetti Unix Sys V Rel.4.0
                11) Siemens Unix (RISC)
                12) Unisys Unix Sys V Rel.4.0
                13) DEC OSF/1
                14) Linux

On which Operating System are you installing? [?]14 ← Linuxを選択★14だよ。

----------------------------------------------------------------------------
PowerChute Plus for Unix is able to monitor other hosts.  However, in order
 to monitor other hosts TCP/IP must be installed. If you do not have TCP/IP
installed, answer 'n' to the following question.
----------------------------------------------------------------------------

Do you currently have TCP/IP Installed? [y/n,q] n ← ネットワーク経由で管理しない

----------------------------------------------------------------------------
If you will be using the Motif version of the User Interface on a
monochrome monitor, using the Monochrome Coloring scheme is recommended.

----------------------------------------------------------------------------
                1) Use Default Color scheme
                2) Use Monochrome Color scheme

Which color scheme do you wish to use [1]?1 ← カラーモニター
----------------------------------------------------------------------------
PowerChute Plus for Unix requires complete control of the serial port. No
processes, including gettys, are allowed to be accessing the port.
Therefore, the serial port you select must NOT be enabled for logins. To
ensure that PowerChute Plus for Unix has control of the serial port, make
sure that it is not enabled for logins.  To disable the port for logins
consult the PowerChute Plus for Unix manual.
----------------------------------------------------------------------------

                1) /dev/ttyS0
                2) /dev/ttyS1
                3) Other

Which serial device will be dedicated to PowerChute Plus for Unix [?] 1 ← 接続ポート
----------------------------------------------------------------------------
You should have the black cable, #940-0024C attached to /dev/ttyS0
Please verify.
----------------------------------------------------------------------------

----------------------------------------------------------------------------
Though it is possible to install PowerChute Plus for Unix
in a directory other than the provided default, it is recommended only
under special circumstances.  Please consult the documentation.
----------------------------------------------------------------------------

Where do you wish to install PowerChute Plus for Unix?
[/usr/lib/powerchute] ← インストール先がここでよければリターン

----------------------------------------------------------------------------
Command files may be executed with root privileges or with the
privileges you assign to the pwrchute account (allowing you to
customize command file execution according to your system
requirements).
----------------------------------------------------------------------------

Do you want to execute command files as root? [y/n,q]  y ← yを選択

----------------------------------------------------------------------------
E-mail may be sent with root privileges or with the privileges you
assign to the pwrchute account.
----------------------------------------------------------------------------

Do you want to send e-mail as root? [y/n,q] y ← yを選択

----------------------------------------------------------------------------
Pwrchute help now comes in an HTML format.
----------------------------------------------------------------------------

Would you like to install HTML help? [y/n,q] y ← yを選択

Please enter your web browser executable name. This value is
case sensitive and should not include the path.
[netscape]/usr/bin/mozilla ← ヘルプを表示するブラウザがnetscapeでよければリターン、違うのでパスを入れる。

Web browser executable found at /usr/bin/mozilla

次のように設定一覧が表示される。
----------------------------------------------------------------------------
PRODUCT                   : PowerChute Plus for Unix
INSTALL USER INTERFACE    : TRUE
INSTALL DAEMON            : TRUE
OPERATING SYSTEM          : Linux
INSTALL PATH              : /usr/lib/powerchute
PATH TO MOUNTED CD ROM    : /mnt/cdrom/UPS/APC
DEDICATED TTY             : /dev/ttyS0
UPS TYPE                  : Smart-UPS
Measure-UPS INSTALLED     : FALSE
PREVIOUS VERSION FOUND    : FALSE
INSTALLING AS ROOT        : TRUE
TCP/IP Installed          : FALSE
RUN COMMAND FILES AS ROOT : TRUE
SEND EMAIL AS ROOT        : TRUE
WEB BROWSER               : /usr/bin/mozilla
----------------------------------------------------------------------------

Are the above selections correct? [y/n,q]y ← 上記設定に問題なければy

Installing PowerChute Plus for Unix from /mnt/cdrom/UPS/APC...
Extracting files.....
...............................
Modifying powerchute.ini file
Eeproms okay.

----------------------------------------------------------------------------
PowerChute Plus for Unix Installation complete.  You will need to reboot
in order to start the application (リブートしろとのこと)
----------------------------------------------------------------------------
#

# umount  /mnt/cdrom

# reboot

CDを取り出す。

(APC社:スケジュール運転時に自動リブート出来ない場合があります。
この場合は BIOS で APM を解除して、ハードスイッチと同等の形式に変更する必要がありますが、
マシンにより変更ができない場合もあります。
上記により、
BIOS の Power Management Mode : Disabled にした。(APM(ACPI),ACPMの無効)
Power Management Mode を Enabled で問題ない場合 Disabled にする必要はない。)

参考:
ASUS.P2B-Dではさらに下記のようにしないとスケジュール運転できない。
PWR Up On Modem Act   : Enabled にした。
AC PWR Loss Restart   : Enabled にした。(電源復帰時にパソコンがONになる。)

rootでログイン

UPSとのCommunicationが確立しているか確認

# top

_upsd コマンドが動作してUPSとのCommunicationが確立していることがわかる。

PowerChuteでUPSの状況確認

# startx

Xターミナル・エミュレーターで

# cd /usr/lib/powerchute

# ./xpowerchute

数秒後詳細が確認できる。

●ftpサーバーを使えるように

# vi /etc/proftpd.conf

上から30行目ぐらいの位置、
 Allow to resume not only the downloads but the uploads too
AllowRetrieveRestart            on
AllowStoreRestart               on
の下

# To prevent DoS attacks, set the maximum number of child processes
# to 30.  If you need to allow more than 30 concurrent connections
の上ぐらいにアクセスできるIPを決める行を追加します。

次のように全てを受け付けないようにしてから、
通したいIPを
Allow fromの右側に記述します。
記述します。
<Limit LOGIN>
  Order Allow, Deny
  Allow from ???.???.???.???
  Allow from ???.???.???.???
  Deny from All
</Limit>

自動起動の設定
# chkconfig proftpd on

# reboot (上記restart不要)
して確認

●postgresql8.2.5.Linux設定

参考 select version();
Turbolinux 8Serverは、PostgreSQL 7.2.2 on i686-pc-linux-gnu, compiled by GCC 2.96
Turbolinux10Serverは、PostgreSQL 7.4.5 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.3.3 (Turbolinux 3.3.3-5)
Turbolinux11Serverは、PostgreSQL 8.2.5 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 4.2.1 (Turbolinux 4.2.1-1)

PostgreSQLはインストール時に追加インストールした。

pg_hba.confなどのデフォルトファイルは存在しないで
一度postgresqlを動作(Initializing database)させて作られたものを編集

(注意:
 「# initdb」をやっていけない。
 基本データベースは
 最初の起動で自動生成されるので、
 自分で「# initdb」をする必要がない。
 やってしまったら起動できなくなるが、
 /var/lib/pgsql/dataディレクトリ内の
 ファイルやディレクトリを全部削除してから
 # rm -r /var/lib/pgsql/data/*
 # rm -rf /var/lib/pgsql/data/*
 起動で自動生成される。


# /etc/init.d/postgresql start

Initializing database:                                                    OK
Starting postgresql service:                                              OK

# /etc/init.d/postgresql stop
 
# vi /var/lib/pgsql/data/pg_hba.conf の下行IPv4の項目に自LANグループを追加

# IPv4-style local connections:
host    all         all         127.0.0.1/32         ident sameuser
# 自分の環境のlanグループで共有する1行を追加
host    all         all         ???.???.???.???   255.255.255.0     trust

追加記述だけではアクセス許可できない、再起動が必要。
# /etc/rc.d/init.d/postgresql restart

(データ保存ディレクトリをRAIDストレージなどに変更した場合の参考例、
 # su -
 # mkdir /usr/pgsql
 # mkdir /usr/pgsql/data
 # chown -R postgres:postgres /usr/pgsql
 # chown -R postgres:postgres /usr/pgsql/data
 # exit
 )

上記で設定したIPを通過してアクセスできるようにする。

# vi /var/lib/pgsql/data/postgresql.conf

#listen_addresses = 'localhost'         # what IP address(es) to listen on;
上記を下記に書き換える。
listen_addresses = '*'

自動起動の設定
# chkconfig postgresql on

# reboot (上記restart不要)
して確認

●postgresql8.2.5.本体設定

PostgreSQL用のユーザー設定する。

# passwd postgres

# adduser dbman
# passwd dbman

postgresでログイン、

説明 createuser ユーザー
1項目yはスパーユーザー権限(一般的にn)
2項目yはデータベース新構築許可(一般的にy)
3項目yはスパーユーザーロール権限(一般的にn)

一般的なユーザーの例
$ createuser dbman
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) n

後述のバックアップCRONを実行させるユーザーを作るのならば
(バキュームコマンドを含んでいるため)
Shall the new role be a superuser? (y/n) y

(dropuser でユーザー削除できる 。)

●DBのテーブルを初めて作る。

dbmanでログイン

$ createdb -E EUC_JP sampledb

確認
$ psql -l
        List of databases
   Name    |  Owner   | Encoding
-----------+----------+-----------
 postgres  | postgres | SQL_ASCII
 sampledb  | dbman    | EUC_JP
 template0 | postgres | SQL_ASCII
 template1 | postgres | SQL_ASCII
(4 rows)

テーブルの作成
作ろうとするシステムに適したフィールドを並びを設計してください。

$ vi mktb_sample.sql
create table sampletb (
     uniqk       varchar(100),
     kazu       int4,
     mozi       text);
create unique index sampletb_index on sampletb(uniqk);

上記の例では
最初のコマンドで、
1列目がキーとしてキャラクター100文字,
2列目が値,
3列目が可変長テキストです。
を設定し、
次のコマンドで、
1列目を重複不可のキーとして設定しています。

(実際は業務に応じて100列前後にまとめます。)

C言語(gcc)では可変長テキストを扱うのに、とても手間がかかります。
レコード設計で列を並べたとき、可変長テキスト、上記例のように
     mozi       text);
を末尾に配置するのがコツです。残りの
     uniqk       varchar(100),
     kazu       int4,
までの列を構造体固定長宣言して演算させるとプログラミングが楽になります。
生産性が高くなくては仕事になりませからん。

テーブルを作る。

$ psql sampledb < mktb_sample.sql

これでWindowsのVisualBasic2008等からデータベースを扱うことが出来ます。

●DBのテーブルを作る、リカバリ。

一般的にDBのテーブルを最初から作ることは希で、
元となるDBを持つコンピュータからコピーしてくる。
$ pg_dump motodb > motodb.txt

今構築しているコンピュータに motodb.txt をコピーしておいて
$ createdb -E EUC_JP motodb
$ psql -d motodb -f motodb.txt

●自動バックアップCRONの設定

☆DBのユーザーでログイン

bashの場所を調べる。
$ which bash
/bin/bash

プログラムを作る。
$ vi backup.cron

----------------------------------------------------------------------------------------
#!/bin/bash
#
#echo This Proguram is backup.cron by dbman.

filename=$(date +%Y%m%d.txt)
pg_dump データーベースの名前 > $filename
vacuumdb -a -f
----------------------------------------------------------------------------------------

ファイルをプログラムとします。
$ chmod u+x backup.cron

実行してみます。
$ ./backup.cron

backup.cronの場所をsu -で調べる。
# find / -name backup.cron
/home/dbman/backup.cron

cronに登録
$ crontab -e

毎日午前1時にバックアップ
0 1 * * * /home/dbman/backup.cron
上記1行を記述します。

参考例
# 毎日午前6時10分にcommandを実行します。
10 6 * * * command

# 30分ごとにcommandを実行します。
0,30 * * * * command

# 2時間ごとにcommandを実行します。
0 */2 * * * command

# 毎週月曜日午前0時にcommandを実行します。
0 0 * * Mon command

# 毎月1日午前0時にcommandを実行します。
0 0 1 * * command

# 月曜日から金曜日の,午前11時にcommandを実行します。
0 11 * * mon-fri command

# 1月1日の午前0時にcommandを実行します。
0 0 1 jan * command

内容の確認
$ crontab -l

# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.XXXX3sriAE installed on Mon Jan  7 14:11:49 2008)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
0 1 * * * /home/dbman/backup.cron

cron のデーモンは, /etc/cron.d 以下のファイルや /etc/crontab, /var/spool/cron 以下をチェックして変更があれば読み込まれますので, 設定変更した場合でもデーモンを再起動する必要はありません.

cronの削除は、以下のコマンドを実行する。
$ crontab -r
$ crontab -l
no crontab for nori

●その他

もう10年ぐらい何台ものPostgreSQLを運転していますが、
新旧バージョンを問わずPostgreSQLデータベースが壊れたことがありません。
大変扱いやすいデーターベースです。

one.