UJP - 技術情報1

Life is fun and easy!

不正IP報告数

Okan Sensor
 
メイン
ログイン
ブログ カテゴリ一覧

PostgreSQLの起動と停止

PostgreSQLの起動と停止


0.改訂履歴

  • 2005.09.29 新規作成

1.はじめに

 このドキュメントでは,PostgreSQLの起動と終了手順を説明する. この資料ではpostmasterを直接起動する方法は用いずにpg_ctlを使っている. また,簡単に起動するための設定などについても記述している.

2.データベースを起動したり停止してみる

  • PostgreSQLの起動は,pg_ctlコマンドで実行する.
  • コマンドを探す.
[postgres@mars database]$ locate pg_ctl | grep bin
/usr/local/pgsql/bin/pg_ctl
[postgres@mars database]$
  • データディレクトリとログファイルを指定して,データベースを起動する.
[postgres@mars database]$ /usr/local/pgsql/bin/pg_ctl start ¥
> -D /usr/local/pgsql/database ¥
> -l /usr/local/pgsql/database/log/postgresql.log
postmaster successfully started
[postgres@mars database]$
  • 起動したプロセスを確認する.
[postgres@mars database]$ ps -ef | grep pg 
postgres 21450     1  0 16:53 pts/0    00:00:00 /usr/local/pgsql/bin/postmaster -D /usr/
local/pgsql/database
postgres 21459 20946  0 16:56 pts/0    00:00:00 grep pg
[postgres@mars database]$  
  • pg_ctlコマンドを利用して,ステータスを確認する.
[postgres@mars database]$ /usr/local/pgsql/bin/pg_ctl status ¥
> -D /usr/local/pgsql/database
pg_ctl: postmaster is running (PID: 21450)
Command line was:
/usr/local/pgsql/bin/postmaster '-D' '/usr/local/pgsql/database'
[postgres@mars database]$ 
[postgres@mars database]$ 
  • ログファイルの中身を確認する.
[postgres@mars database]$ cat /usr/local/pgsql/database/log/postgresql.log 
LOG:  database system was shut down at 2005-09-14 16:37:12 JST
LOG:  checkpoint record is at 0/9B0B8C
LOG:  redo record is at 0/9B0B8C; undo record is at 0/0; shutdown TRUE
LOG:  next transaction ID: 536; next OID: 17142
LOG:  database system is ready
[postgres@mars database]$  
  • 正常に起動できたことが確認できた.
  • データベースを停止する.
[postgres@mars database]$ /usr/local/pgsql/bin/pg_ctl stop ¥
> -D /usr/local/pgsql/database
waiting for postmaster to shut down.....done
postmaster successfully shut down
[postgres@mars database]$  
  • 停止後,ログを確認する.
[postgres@mars database]$ cat /usr/local/pgsql/database/log/postgresql.log 
LOG:  database system was shut down at 2005-09-14 16:37:12 JST
LOG:  checkpoint record is at 0/9B0B8C
LOG:  redo record is at 0/9B0B8C; undo record is at 0/0; shutdown TRUE
LOG:  next transaction ID: 536; next OID: 17142
LOG:  database system is ready
LOG:  received smart shutdown request
LOG:  shutting down
LOG:  database system is shut down
[postgres@mars database]$  

5.起動・停止コマンドの準備

  • pg_ctlを使ってオプションをつけずに起動してみる.
[postgres@mars database]$ pg_ctl start
pg_ctl: no database directory specified and environment variable PGDATA unset
Try "pg_ctl --help" for more information.
[postgres@mars database]$
  • 環境変数PGDATAにデータベース格納ディレクトリを指定すればよいことがわかった.
  • 環境変数を設定する.
[postgres@mars database]$ export PGDATA=/usr/local/pgsql/database
[postgres@mars database]$
  • ログファイルを指定して,実行してみる.
[postgres@mars database]$ pg_ctl start ¥
> -l /usr/local/pgsql/database/log/postgresql.log
postmaster successfully started
[postgres@mars database]$
  • 起動したようである.
  • ステータスを確認する.
[postgres@mars database]$ pg_ctl status
pg_ctl: postmaster is running (PID: 21649)
Command line was:
/usr/local/pgsql/bin/postmaster
[postgres@mars database]$
  • このとき,データベース格納ディレクトリの指定は不要であることが確認できた.
  • データベースを停止する.
[postgres@mars database]$ pg_ctl stop
waiting for postmaster to shut down......done
postmaster successfully shut down
[postgres@mars database]$
  • PGDATA環境変数が生きているようである.
  • ログファイル名を入力するのも面倒なので,環境変数PGDATAと,aliasを定義する.
[postgres@mars postgres]$ cat .bashrc
# .bashrc

# User specific aliases and functions

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
PATH=/usr/local/pgsql/bin:$PATH
export LANG=C
export PGDATA=/usr/local/pgsql/database
alias pg_start="pg_ctl start -l /usr/local/pgsql/database/log/postgresql.log"
alias pg_stop="pg_ctl stop" 
[postgres@mars postgres]$  
  • 設定したシェル環境を反映し,確認する.
[postgres@mars postgres]$ . .bashrc
[postgres@mars postgres]$ env | grep PGDATA
PGDATA=/usr/local/pgsql/database
[postgres@mars postgres]$ alias|grep pg_
alias pg_start='pg_ctl start -l /usr/local/pgsql/database/log/postgresql.log'
alias pg_stop='pg_ctl stop'
[postgres@mars postgres]$
  • これで,postgresユーザでログインした際には,簡単にデータベースの起動と停止ができるようになる.
  • 試してみる.
[postgres@mars postgres]$ pg_start
pg_ctl: Another postmaster may be running.  Trying to start postmaster anyway.
pg_ctl: cannot start postmaster
Examine the log output.
[postgres@mars postgres]$ pg_stop
waiting for postmaster to shut down......done
postmaster successfully shut down
[postgres@mars postgres]$ pg_start
postmaster successfully started
[postgres@mars postgres]$ pg_ctl status
pg_ctl: postmaster is running (PID: 8867)
Command line was:
/usr/local/pgsql/bin/postmaster
[postgres@mars postgres]$  
  • 正しく動いていることが確認できた.


広告スペース
Google