实现PostgreSQL自启动

在手动安装(针对源码编译PG或者是解压缩版安装PG的情形)情况下,PG并不是在开机的情况下自动启动,在关机的情况下自动停止,作为DBA人员来说,显然这样的情形是无法接受的。


1. windows下的服务自启动


在Windows下, 可以使用pg_ctl命令生成PostgreSQL服务,并让它自启动。实际上,安装版本也是这么做的。  我们不妨看看pg_ctl命令的详细帮助先:

[cpp]  view plain  copy
  1. D:\pg921>pg_ctl --help  
  2. pg_ctl is a utility to initialize, start, stop, or control a PostgreSQL server.  
  3.   
  4. Usage:  
  5.   pg_ctl init[db]               [-D DATADIR] [-s] [-o "OPTIONS"]  
  6.   pg_ctl start   [-w] [-t SECS] [-D DATADIR] [-s] [-l FILENAME] [-o "OPTIONS"]  
  7.   pg_ctl stop    [-W] [-t SECS] [-D DATADIR] [-s] [-m SHUTDOWN-MODE]  
  8.   pg_ctl restart [-w] [-t SECS] [-D DATADIR] [-s] [-m SHUTDOWN-MODE]  
  9.                  [-o "OPTIONS"]  
  10.   pg_ctl reload  [-D DATADIR] [-s]  
  11.   pg_ctl status  [-D DATADIR]  
  12.   pg_ctl promote [-D DATADIR] [-s]  
  13.   pg_ctl kill    SIGNALNAME PID  
  14. <strong>  pg_ctl register   [-N SERVICENAME] [-U USERNAME] [-P PASSWORD] [-D DATADIR]  
  15.                     [-S START-TYPE] [-w] [-t SECS] [-o "OPTIONS"]  
  16.   pg_ctl unregister [-N SERVICENAME]</strong>  
  17.   
  18. Common options:  
  19.   -D, --pgdata=DATADIR   location of the database storage area  
  20.   -s, --silent           only print errors, no informational messages  
  21.   -t, --timeout=SECS     seconds to wait when using -w option  
  22.   -V, --version          output version information, then exit  
  23.   -w                     wait until operation completes  
  24.   -W                     do not wait until operation completes  
  25.   -?, --help             show this help, then exit  
  26. (The default is to wait for shutdown, but not for start or restart.)  
  27.   
  28. If the -D option is omitted, the environment variable PGDATA is used.  
  29.   
  30. Options for start or restart:  
  31.   -c, --core-files       not applicable on this platform  
  32.   -l, --log=FILENAME     write (or append) server log to FILENAME  
  33.   -o OPTIONS             command line options to pass to postgres  
  34.                          (PostgreSQL server executable) or initdb  
  35.   -p PATH-TO-POSTGRES    normally not necessary  
  36.   
  37. Options for stop or restart:  
  38.   -m, --mode=MODE        MODE can be "smart""fast", or "immediate"  
  39.   
  40. Shutdown modes are:  
  41.   smart       quit after all clients have disconnected  
  42.   fast        quit directly, with proper shutdown  
  43.   immediate   quit without complete shutdown; will lead to recovery on restart  
  44.   
  45. Allowed signal names for kill:  
  46.   ABRT HUP INT QUIT TERM USR1 USR2  
  47.   
  48. Options for register and unregister:  
  49.   -N SERVICENAME  service name with which to register PostgreSQL server  
  50.   -P PASSWORD     password of account to register PostgreSQL server  
  51.   -U USERNAME     user name of account to register PostgreSQL server  
  52.   -S START-TYPE   service start type to register PostgreSQL server  
  53.   
  54. Start types are:  
  55.   auto       start service automatically during system startup (default)  
  56.   demand     start service on demand  
  57.   
  58. Report bugs to <[email protected]>.  
从上边可以看出,pg_ctl register用于生成服务,而pg_ctl unregister -N <服务名>用于删除一个服务。

如:

D:\pg921>pg_ctl register -N pg921 -D d:\pg921\data -S auto -w -t 10  -l d:/pg921/log/pg921.log -o "-p 5433"

此命令,即是要生成一个服务:pg921, 启动方式: -S auto, 自启动,如果想生成手动启动,就用-S demand来指定。

-t 10,意指等待10秒钟, 实际上可以设定的长一些(在生产环境中). 

-l d:/pg921/log/pg921.log, 指定生成的日志文件的位置。

-o "-p 5433", 将服务端口号改为5433。

验证一下上述命令生成的效果:

[cpp]  view plain  copy
  1. D:\pg921>net start pg921  
  2. The pg921 service is starting.  
  3. The pg921 service was started successfully.  
  4.   
  5.   
  6. D:\pg921>psql -p 5433 iihero  
  7. psql (9.2.1)  
  8. Type "help" for help.  
  9.   
  10. iihero=# \q  


2. Linux下的服务自启动


在Linux下,我们需要写一个自启动的脚本,至少支持两个命令选项: start 和 stop,并将这个脚本建立适当的链接。我们就以Ubuntu10为例,

先看看系统有没有chkconfig命令工具:

xionghe@seanlinux2:~$ chkconfig
程序“chkconfig”尚未安装。  您可以使用以下命令安装:
sudo apt-get install chkconfig
xionghe@seanlinux2:~$ sudo apt-get install chkconfig

脚本内容如下: 放入目录/etc/init.d目录下边

[plain]  view plain  copy
  1. #! /bin/sh  
  2.   
  3. # Installation prefix  
  4. prefix=/home/xionghe/pgsql  
  5.   
  6. # Data directory  
  7. PGDATA="/home/xionghe/pgsql/data"  
  8.   
  9. # Who to run the postmaster as, usually "postgres".  (NOT "root")  
  10. PGUSER=xionghe  
  11.   
  12. # Where to keep a log file  
  13. PGLOG="$PGDATA/serverlog"  
  14.   
  15. # It's often a good idea to protect the postmaster from being killed by the  
  16. # OOM killer (which will tend to preferentially kill the postmaster because  
  17. # of the way it accounts for shared memory).  Setting the OOM_ADJ value to  
  18. # -17 will disable OOM kill altogether.  If you enable this, you probably want  
  19. # to compile PostgreSQL with "-DLINUX_OOM_ADJ=0", so that individual backends  
  20. # can still be killed by the OOM killer.  
  21. #OOM_ADJ=-17  
  22.   
  23. ## STOP EDITING HERE  
  24.   
  25. # The path that is to be used for the script  
  26. PATH=/home/xionghe/pgsql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin  
  27.   
  28. # What to use to start up the postmaster.  (If you want the script to wait  
  29. # until the server has started, you could use "pg_ctl start -w" here.  
  30. # But without -w, pg_ctl adds no value.)  
  31. DAEMON="$prefix/bin/postmaster"  
  32.   
  33. # What to use to shut down the postmaster  
  34. PGCTL="$prefix/bin/pg_ctl"  
  35.   
  36. set -e  
  37.   
  38. # Only start if we can find the postmaster.  
  39. test -x $DAEMON ||  
  40. {  
  41.     echo "$DAEMON not found"  
  42.     if [ "$1" = "stop" ]  
  43.     then exit 0  
  44.     else exit 5  
  45.     fi  
  46. }  
  47.   
  48.   
  49. # Parse command line parameters.  
  50. case $1 in  
  51.   start)  
  52.     echo -n "Starting PostgreSQL: "  
  53.     test x"$OOM_ADJ" != x && echo "$OOM_ADJ" > /proc/self/oom_adj  
  54.     su - $PGUSER -c "$DAEMON -D '$PGDATA' &" >>$PGLOG 2>&1  
  55.     echo "ok"  
  56.     ;;  
  57.   stop)  
  58.     echo -n "Stopping PostgreSQL: "  
  59.     su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s -m fast"  
  60.     echo "ok"  
  61.     ;;  
  62.   restart)  
  63.     echo -n "Restarting PostgreSQL: "  
  64.     su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s -m fast -w"  
  65.     test x"$OOM_ADJ" != x && echo "$OOM_ADJ" > /proc/self/oom_adj  
  66.     su - $PGUSER -c "$DAEMON -D '$PGDATA' &" >>$PGLOG 2>&1  
  67.     echo "ok"  
  68.     ;;  
  69.   reload)  
  70.         echo -n "Reload PostgreSQL: "  
  71.         su - $PGUSER -c "$PGCTL reload -D '$PGDATA' -s"  
  72.         echo "ok"  
  73.         ;;  
  74.   status)  
  75.     su - $PGUSER -c "$PGCTL status -D '$PGDATA'"  
  76.     ;;  
  77.   *)  
  78.     # Print help  
  79.     echo "Usage: $0 {start|stop|restart|reload|status}" 1>&2  
  80.     exit 1  
  81.     ;;  
  82. esac  
  83.   
  84. exit 0  

建立相应链接:


root@seanlinux2:/etc# ln -s /etc/init.d/postgresql /etc/rc0.d/K02postgresql
root@seanlinux2:/etc# ln -s /etc/init.d/postgresql /etc/rc1.d/K02postgresql
root@seanlinux2:/etc# ln -s /etc/init.d/postgresql /etc/rc2.d/K02postgresql
root@seanlinux2:/etc# ln -s /etc/init.d/postgresql /etc/rc3.d/K98postgresql
root@seanlinux2:/etc# ln -s /etc/init.d/postgresql /etc/rc4.d/K98postgresql
root@seanlinux2:/etc# ln -s /etc/init.d/postgresql /etc/rc5.d/K98postgresql

猜你喜欢

转载自blog.csdn.net/qq_24084925/article/details/80118586