PostgreSQL服务器启动和关闭方法介绍


1. 启动数据库服务器(posgres用户):

  1. [postgres@localhost bin]$ postgres -D /opt/postgresql/data/ > /opt/postgresql/log/pg_server.log 2>&1 &  
  2. [1] 4508  

当然如果设置了环境变量

  1. PGDATA=/opt/postgresql/data  
  2. export PGDATA  

后,可使用pg_ctl工具进行启动:

  1. [postgres@localhost log]$ pg_ctl start -l /opt/postgresql/log/pg_server.log  
  2. pg_ctl: another server might be running; trying to start server anyway  
  3. pg_ctl: could not start server  
  4. Examine the log output.  
  5. [postgres@localhost log]$  

因为之前已经启动,所以打印“another server might be running”。此时,查看日志,有如下信息:

  1. [postgres@localhost log]$ cat pg_server.log  
  2. FATAL:  lock file "postmaster.pid" already exists  
  3. HINT:  Is another postmaster (PID 4491) running in data directory "/opt/postgresql/data"?  
  4. [postgres@localhost log]$  

当然,最简的启动方式是:

  1. [postgres@localhost ~]$ pg_ctl start  
  2. server starting  
  3. [postgres@localhost ~]$ LOG:  database system was shut down at 2011-07-09 13:58:00 CST  
  4. LOG:  autovacuum launcher started  
  5. LOG:  database system is ready to accept connections  

如果要在操作系统启动时就启动PG,可以在/etc/rc.d/rc.local 文件中加以下语句:

  1. /opt/postgresql/bin/pg_ctl start -l /opt/postgresql/log/pg_server.log -D /opt/postgresql/data  

2.关闭服务器

最简单方法:

  1. [postgres@localhost ~]$ pg_ctl stop  
  2. waiting for server to shut down.... done  
  3. server stopped  

与Oracle相同,在关闭时也可采用不同的模式,简介如下:

  1. SIGTERM   
  2. 不再允许新的连接,但是允许所有活跃的会话正常完成他们的工作,只有在所有会话都结束任务后才关闭。这是智能关闭。  
  3.   
  4. SIGINT   
  5. 不再允许新的连接,向所有活跃服务器发送 SIGTERM(让它们立刻退出),然后等待所有子进程退出并关闭数据库。这是快速关闭。  
  6.   
  7. SIGQUIT   
  8. 令 postgres 向所有子进程发送 SIGQUIT 并且立即退出(所有子进程也会立即退出),而不会妥善地关闭数据库系统。这是立即关闭。这样做会导致下次启动时的恢复(通过重放 WAL 日志)。我们推荐只在紧急的时候使用这个方法。  
  9.   
  10. SIGKILL   
  11. 此选项尽量不要使用,这样会阻止服务器清理共享内存和信号灯资源,那样的话你只能在启动服务器之前自己手工做这件事。另外,SIGKILL 直接把 postgres 杀掉,而不会等它把信号中继给它的子进程,因此我们还需要手工杀掉每个独立子进程。  

使用方法举例:

  1. [postgres@localhost ~]$ pg_ctl stop -o SIGTERM  
  2. LOG:  received smart shutdown request  
  3. LOG:  autovacuum launcher shutting down  
  4. waiting for server to shut down....LOG:  shutting down  
  5. LOG:  database system is shut down  
  6.  done  
  7. server stopped  
  8. [postgres@localhost ~]$  

最快速关闭方法:kill postgres 进程

  1. [postgres@localhost ~]$ kill -INT `head -1 /opt/postgresql/data/postmaster.pid`  
  2. [postgres@localhost ~]$ LOG:  received fast shutdown request  
  3. LOG:  aborting any active transactions  
  4. LOG:  autovacuum launcher shutting down  
  5. LOG:  shutting down  
  6. LOG:  database system is shut down  

 附:postgre启动后的进程,如下:

  1. [postgres@localhost ~]$  ps -ef|grep post  
  2. root      4609  4543  0 13:57 pts/2    00:00:00 su - postgres  
  3. postgres  4610  4609  0 13:57 pts/2    00:00:00 -bash  
  4. postgres  4724     1  0 14:08 pts/2    00:00:00 /opt/postgresql/bin/postgres  
  5. postgres  4726  4724  0 14:08 ?        00:00:00 postgres: writer process  
  6. postgres  4727  4724  0 14:08 ?        00:00:00 postgres: wal writer process  
  7. postgres  4728  4724  0 14:08 ?        00:00:00 postgres: autovacuum launcher process  
  8. postgres  4729  4724  0 14:08 ?        00:00:00 postgres: stats collector process  
  9. postgres  4752  4610  0 14:11 pts/2    00:00:00 ps -ef  
  10. postgres  4753  4610  0 14:11 pts/2    00:00:00 grep post  
  11. [postgres@localhost ~]$  

相关内容