PC上にPostgreSQLをセットアップする

 以前一度Win2k上に実施したが忘れた。そのマシンは負荷の大きいjobを回していて使い物にならないので、あらためて手持ちのPCにインストールする。理由は、レンタルサーバのものはtelnet接続による作業が必要でアクセスに制約があり練習も兼ねるのには効率が悪すぎる。ローカルで十分経験を積んでからそれをどう公開の場に展開するかを考える。取り敢えず、以前作業で参照したブックマークに従って復習してみる。今回はXP。

postgresql パッケージのバージョン確認

$ date
Tue Jul 12 10:21:30 2005
$ cygcheck -cd cygwin ash crypt cygrunsrv libncurses7 libreadline5 openssl zlib postgresql
Cygwin Package Information
Package Version
ash 20040127-1
crypt 1.1-1
cygrunsrv 1.01-1
cygwin 1.5.14-1
libncurses7 5.3-4
libreadline5 4.3-5
openssl 0.9.7g-1
postgresql 7.4.5-1
zlib 1.2.2-1

作業前点検

 そのうち、やったこと。

  1. システム環境変数 Path 確認。
  2. システム環境変数 CYGWIN に、文字列 "server" を追加。
  3. システム環境変数を追加・修正した後、Windowsを再起動。

/etc/cygserver.conf 生成

  1. cygserver-configスクリプトを実行すると生成されるようだ。
  2. この作業は一回だけでよいらしい。
  3. なお、cygserver.confkern.ipc.semmns の値を変更する必要があるかも知れないとのこと(たしか2kの時も数値を弄らなければならなかった記憶あり)。

$ ls /etc/*.conf
/etc/clamd.conf* /etc/freshclam.conf* /etc/mime.conf* /etc/xinetd.conf*
/etc/esd.conf* /etc/inetd.conf /etc/proftpd.conf
/etc/exim.conf /etc/lftp.conf* /etc/squid.conf*

$ date
Tue Jul 12 12:07:22 2005

$ cygserver-config
Generating /etc/cygserver.conf file


Warning: The following function requires administrator privileges!

Do you want to install cygserver as service?
(Say "no" if it's already installed as service) (yes/no) no

Further configuration options are available by editing the configuration
file /etc/cygserver.conf. Please read the inline information in that
file carefully. The best option for the start is to just leave it alone.

Please keep in mind, that a client application which wants to use
the services provided by cygserver *must* have the environment variable
CYGWIN set so that it contains the word "server". So, if you don't
need any other special CYGWIN setting, just set it to "server".

It is advisable to add this setting to the Windows system environment.

Basic Cygserver configuration finished. Have fun!


$ ls /etc/*.conf
/etc/clamd.conf* /etc/exim.conf /etc/lftp.conf* /etc/squid.conf*
/etc/cygserver.conf /etc/freshclam.conf* /etc/mime.conf* /etc/xinetd.conf*
/etc/esd.conf* /etc/inetd.conf /etc/proftpd.conf

$ cat /etc/cygserver.conf
# cygserver.conf, Copyright(C) 2003 Red Hat Inc.
#
# Contains configurable parameters for the cygserver.
#
# The format of this file is easy. Lines beginning with a hash `#' are
# comments and ignored. Lines consisting of only whitespaces are ignored.
# Any other line is a setting for cygserver.
# A setting consists of a name/value pair, separated by whitespace.
# Each line must only consist of one name/value pair.
# Lines must not be longer than 1023 characters.
#
# Some settings can be overridden by a command line switch. If so, it's
# mentioned below.
#
# Settings which are commented out will use the default values. These are
# mentioned below, too.

# kern.srv.cleanup_threads: No. of cygserver threads used for cleanup tasks.
# Default: 2, Min: 1, Max: 16, command line option -c, --cleanup-threads
#kern.srv.cleanup_threads 2

# kern.srv.request_threads: No. of cygserver threads used to serve
# application requests.
# Default: 10, Min: 1, Max: 64, command line option -r, --request-threads
#kern.srv.request_threads 10

# kern.srv.msgqueues: Determines whether XSI Message Queue support should be
# started, "yes" (or "true", "y", "t", "1") or "no" (or "false", "n", "f", "0").

# These values are valid for all binary type options.
# Default is "yes". Command line option -q, --no-msgqueues
#kern.srv.msgqueues yes

# kern.srv.semaphores: Determines whether XSI Semaphore support should be
# started. Default is "yes". Command line option -s, --no-semaphores
#kern.srv.semaphores yes

# kern.srv.sharedmem: Determines whether XSI Shared Memory support should be
# started. Default is "yes". Command line option -m, --no-sharedmem
#kern.srv.sharedmem yes

# LOGGING

# kern.log.syslog: Determines whether logging should go to the syslog,
# Default is "yes", if stderr is no tty, "no" otherwise.
# Command line option -y, --syslog or -Y, --no-syslog.
#kern.log.syslog no

# kern.log.stderr: Determines whether logging should go to stderr,
# Default is "yes", if stderr is a tty, "no" otherwise.
# Command line option -e, --stderr or -E, --no-stderr.
#kern.log.stderr no

# kern.log.level: Logging level. Valid values are 1 to 7 with a bigger
# value emitting more logging output. Default level is 6.
# Command line option -l, --log-level.
#kern.log.level 6

# kern.log.debug: Determines whether debug output should be printed to stderr.
# Default is "no". Command line option -d, --debug
#kern.log.debug no

# XSI message queue parameters.
#
# Each message is broken up and stored in segments that are msgssz bytes
# long. For efficiency reasons, this should be a power of two. Also,
# it doesn't make sense if it is less than 8 or greater than about 256.

# kern.ipc.msgseg: Maximum no. of message queue segments hold concurrently.
# Default: 2048, Min: 256, Max: 65535
#kern.ipc.msgseg 2048

# kern.ipc.msgssz: Size of segment in bytes. Must be a power of 2 value.
# Default: 8, Min: 8, Max: 1024
#kern.ipc.msgssz 8

# kern.ipc.msgmnb: Maximum no. of bytes in a single message queue.
# Default: 2048, Min: 1, Max: 65535
#kern.ipc.msgmnb 2048

# kern.ipc.msgmni: Maximum no. of message queue identifiers hold concurrently.
# Default: 40, Min: 1, Max: 1024
#kern.ipc.msgmni 40

# kern.ipc.msgtql: Maximum no. of messages hold concurrently.
# Default: 40, Min: 1, Max: 1024
#kern.ipc.msgtql 40

# XSI semaphore parameters

# kern.ipc.semmni: Maximum no. of semaphore identifiers hold concurrently.
# Default: 10, Min: 1, Max: 1024
#kern.ipc.semmni 10

# kern.ipc.semmns: Maximum no. of semaphores hold concurrently.
# Default: 60, Min: 1, Max: 1024
#kern.ipc.semmns 60

# kern.ipc.semmnu: Total no. of undo structures hold by server.
# Default: 30, Min: 1, Max: 1024
#kern.ipc.semmnu 30

# kern.ipc.semmsl: Maximum no. of semaphores per semaphore id.
# Default: 60, Min: 1, Max: 1024
#kern.ipc.semmsl 60

# kern.ipc.semopm: Maximum no. of operations per semop call.
# Default: 100, Min: 1, Max: 1024
#kern.ipc.semopm 100

# kern.ipc.semume: Maximum no. of undo entries per process.
# Default: 10, Min: 1, Max: 1024
#kern.ipc.semume 10

# kern.ipc.semvmx: Maximum value of a semaphore.
# Default: 32767, Min: 1, Max: 32767
#kern.ipc.semvmx 32767

# kern.ipc.semaem: Maximum value to adjust on process exit.
# Default: 16384, Min: 1, Max: 32767
#kern.ipc.semaem 16384

# XSI shared memory parameters

# kern.ipc.shmmaxpgs: Maximum pages available for XSI shared memory.
# Changing shmmaxpgs immediately affects setting SHMMAX. The relation
# between shmmaxpgs and SHMMAX is: SHMMAX = shmmaxpgs * getpagesize().
# Default: 8192, Min: 1, Max: 32767
#kern.ipc.shmmaxpgs 8192

# kern.ipc.shmmni: Maximum number of shared memory segments, system wide.
# Default: 192, Min: 1, Max: 32767
#kern.ipc.shmmni 192

# kern.ipc.shmseg: Maximum number of shared memory segments per process.
# Default: 128, Min: 1, Max: 32767
#kern.ipc.shmseg 128

cygserver プロセス起動

$ date
Tue Jul 12 14:52:36 2005

$ /usr/sbin/cygserver & 【バックグラウンドプロセスとして起動】
[1] 3700

$ cygserver: Initialization complete. Waiting for requests.


$ jobs 【★起動確認】
[1]+ Running /usr/sbin/cygserver &

$ ps -a | grep cygserver 【★起動確認】
3700 3744 3700 4024 con 1003 14:53:01 /usr/sbin/cygserver

$ ipcs 【★System V IPC の動作状態を確認】
Message Queues:
T ID KEY MODE OWNER GROUP

Shared Memory:
T ID KEY MODE OWNER GROUP

Semaphores:
T ID KEY MODE OWNER GROUP

PostgreSQL初期化

$ initdb --no-locale -D /var/postgresql/data 【日本語を扱うつもりなので、--no-locale を追加】
The files belonging to this database system will be owned by user "Owner".
This user must also own the server process.

The database cluster will be initialized with locale C.

creating directory /var/postgresql/data... ok
creating directory /var/postgresql/data/base... ok
creating directory /var/postgresql/data/global... ok
creating directory /var/postgresql/data/pg_xlog... ok
creating directory /var/postgresql/data/pg_clog... ok
selecting default max_connections... 40
selecting default shared_buffers... 1000
creating configuration files... ok
creating template1 database in /var/postgresql/data/base/1... 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
creating information schema... ok
vacuuming database template1... ok
copying template1 to template0... ok

Success. You can now start the database server using:

/usr/bin/postmaster -D /var/postgresql/data
or
/usr/bin/pg_ctl -D /var/postgresql/data -l logfile start


$ cd /var
$ ls
./ cache/ lib/ openldap/ run/ tmp/ www.new/
../ cron/ log/ proftpd/ spool/ www/
$ ls
./ cache/ lib/ openldap/ proftpd/ spool/ www/
../ cron/ log/ postgresql/ run/ tmp/ www.new/
$ cd postgresql/
$ cd data/
$ pwd
/var/postgresql/data
$ ls
./ PG_VERSION global/ pg_hba.conf pg_xlog/
../ base/ pg_clog/ pg_ident.conf postgresql.conf

PostgreSQLの postmaster を起動

$ postmaster -D /var/postgresql/data & 【★postmaster を直接起動で常駐させる】
[2] 1264
$
WARNING: dup(0) failed after 3196 successes: Bad file descriptor
LOG: database system was shut down at 2005-07-12 15:42:55
LOG: checkpoint record is at 0/9DDFA0
LOG: redo record is at 0/9DDFA0; undo record is at 0/0; shutdown TRUE
LOG: next transaction ID: 536; next OID: 17142
LOG: database system is ready

$ jobs 【★起動確認】
[1]- Running /usr/sbin/cygserver &
[2]+ Running postmaster -D /var/postgresql/data &

試しに PostgreSQL DB に接続してみる

$ psql template1
Welcome to psql 7.4.5, 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

template1=# \q 【★ '\q' リターン、でpsqlを終了させる。】

PostgreSQLサーバプロセスの停止のしかた

killコマンドを使う方法

プロセスIDを調べる方法

$ cat /var/postgresql/data/postmaster.pid
1264 【←★これがプロセスID】
/var/postgresql/data
5432001 1114112

あるいは、

head -1 /var/postgresql/data/postmaster.pid

プロセスをkillコマンドで停止

$ kill `head -1 /var/postgresql/data/postmaster.pid`
$
LOG: received smart shutdown request
LOG: shutting down
LOG: database system is shut down
[2]+ Done postmaster -D /var/postgresql/data

pg_ctlを使ったpostmasterの起動・停止

起動

$ pg_ctl -D /var/postgresql/data -l /var/log/postmaster.log start
postmaster successfully started
$ cat /var/log/postmaster.log
WARNING: dup(0) failed after 3196 successes: Bad file descriptor
LOG: database system was shut down at 2005-07-12 16:58:55
LOG: checkpoint record is at 0/9DDFE0
LOG: redo record is at 0/9DDFE0; undo record is at 0/0; shutdown TRUE
LOG: next transaction ID: 537; next OID: 17142
LOG: database system is ready

stop モードで停止

$ pg_ctl stop -D /var/postgresql/data
waiting for postmaster to shut down......done
postmaster successfully shut down

cygserverの停止のしかた

プロセスIDを調べる

$ ps -a |grep cygserver
3700 3744 3700 4024 con 1003 14:53:01
【↑★これがプロセスID】

killコマンドで停止

$ kill 3700
$
cygserver: Shutdown finished.
[1]+ Done /usr/sbin/cygserver

cygserverをサービスとして起動・停止する

cygrunsrvを使う

$ cygrunsrv -S cygserver 【★起動】
$ cygrunsrv -Q cygserver 【★確認】
Service cygserver exists
Type : Own Process
Current State : Running 【★動作中】
Controls Accepted : Accept Stop
$ cat /var/log/cygserver.log 【★ログ確認】

$               【★エラーなし。】
$ cygrunsrv -E cygserver 【★停止】
$ cygrunsrv -Q cygserver 【★確認】
Service cygserver exists
Type : Own Process
Current State : Stopped 【★停止中】
Controls Accepted :

netscを使う

$ net start cygserver
CYGWIN cygserver サービスを開始します.
CYGWIN cygserver サービスは正常に開始されました。
$ sc query cygserver

SERVICE_NAME: cygserver
TYPE : 10 WIN32_OWN_PROCESS
STATE : 4 RUNNING 【★動作中】
(STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
$ cat /var/log/cygserver.log 【★ログ確認】

$               【★エラーなし。】
$ net stop cygserver
CYGWIN cygserver サービスを停止中です.
CYGWIN cygserver サービスは正常に停止されました。
$ sc query cygserver

SERVICE_NAME: cygserver
TYPE : 10 WIN32_OWN_PROCESS
STATE : 1 STOPPED 【★停止中】
(NOT_STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
…(他の方法?)
$ sc start cygserver 【★起動】

SERVICE_NAME: cygserver
TYPE : 10 WIN32_OWN_PROCESS
STATE : 2 START_PENDING
(NOT_STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x7d0
PID : 2932
FLAGS :
$ sc stop cygserver 【★停止】

SERVICE_NAME: cygserver
TYPE : 10 WIN32_OWN_PROCESS
STATE : 3 STOP_PENDING
(STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x2
WAIT_HINT : 0x5208

“postgres”ユーザアカウント作成

コントロールパネル⇒ユーザーアカウント⇒新しいアカウントを作成する。