postgresql.init 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #! /bin/sh
  2. # postgresql This is the init script for starting up the PostgreSQL
  3. # server
  4. #
  5. # chkconfig: - 85 15
  6. # description: Starts and stops the PostgreSQL backend daemon that handles \
  7. # all database requests.
  8. # processname: postmaster
  9. # pidfile: /var/run/postmaster.pid
  10. # Version 6.5.3-2 Lamar Owen
  11. # Added code to determine if PGDATA exists, whether it is current version
  12. # or not, and initdb if no PGDATA (initdb will not overwrite a database).
  13. # Version 7.0 Lamar Owen
  14. # Added logging code
  15. # Changed PGDATA.
  16. #
  17. # Version 7.0.2 Trond Eivind Glomsrd <teg@redhat.com>
  18. # use functions, add conditional restart
  19. # Version 7.0.3 Lamar Owen <lamar@postgresql.org>
  20. # Check for the existence of functions before blindly using them
  21. # in particular -- check for success () and failure () before using.
  22. # More Cross-distribution support -- PGVERSION variable, and docdir checks.
  23. # Version 7.1 Release Candidate Lamar Owen <lamar@postgresql.org>
  24. # initdb parameters have changed.
  25. # Version 7.1.2 Trond Eivind Glomsrd <teg@redhat.com>
  26. # Specify shell for su
  27. # Handle stop better - kill unwanted output, make it wait until the database is ready
  28. # Handle locales slightly differently - always using "C" isn't a valid option
  29. # Kill output from database initialization
  30. # Mark messages for translation
  31. # Version 7.1.2-2.PGDG Lamar Owen <lamar.owen@wgcr.org>
  32. # sync up.
  33. # Karl's fixes for some quoting issues.
  34. # Version 7.2b2 Lamar Owen <lamar.owen@wgcr.org>
  35. # version change.
  36. # Version 7.2 final. Lamar Owen <lamar.owen@wgcr.org>
  37. # reload from Peter E.
  38. # Eliminate the pidof postmaster test in stop -- we're using pg_ctl so we don't need pidof.
  39. # Tested the $? return for the stop script -- it does in fact propagate.
  40. # TODO: multiple postmasters.
  41. # VErsion 7.3 Lamar OWen <lamar.owen@ramifordistat.net>
  42. # Multiple postmasters, courtesy Karl DeBisschop
  43. # Version 7.4 HOTTA Michihide <hotta@net-newbie.com>
  44. # Version 8.0 HOTTA Michihide <hotta@net-newbie.com>
  45. # Version 8.1 HOTTA Michihide <hotta@net-newbie.com>
  46. # Version 8.2 Shu KONNO <owa@bg.wakwak.com>
  47. # Version 9.0 IWAI, Masaharu <iwaim.sub@gmail.com>
  48. # fix su(1) paramaters in start() and stop().
  49. # Version 9.4 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net>
  50. # fixed $NAME and warning messages.
  51. # PGVERSION must be replaced real version
  52. # In vine new postgresql.spec, version must be replaced by building
  53. PGVERSION=@pgver@
  54. # Source function library.
  55. INITD=/etc/rc.d/init.d
  56. . $INITD/functions
  57. # Get function listing for cross-distribution logic.
  58. TYPESET=`typeset -f|grep "declare"`
  59. # Get config.
  60. . /etc/sysconfig/network
  61. # Find the name of the script
  62. NAME=postgresql
  63. # Set defaults for port and database directory
  64. PGPORT=5432
  65. export PGDATA=/var/lib/pgsql
  66. if [ -f $PGDATA/PG_VERSION ] && [ -d $PGDATA/base/template1 ]
  67. then
  68. echo "Using old-style directory structure"
  69. else
  70. export PGDATA=/var/lib/pgsql/data
  71. fi
  72. # Override defaults from /etc/sysconfig/pgsql if file is present
  73. [ -f /etc/sysconfig/pgsql/${NAME} ] && . /etc/sysconfig/pgsql/${NAME}
  74. export PGDATA
  75. export PGPORT
  76. export PGOPTS
  77. # Check that networking is up.
  78. # Pretty much need it for postmaster.
  79. [ "${NETWORKING}" = "no" ] && exit 0
  80. [ -f /usr/bin/postmaster ] || exit 0
  81. start(){
  82. PSQL_START=$"Starting ${NAME} service: "
  83. # Check for the PGDATA structure
  84. if [ -f $PGDATA/PG_VERSION ] && [ -d $PGDATA/base ]
  85. then
  86. # Check version of existing PGDATA
  87. if [ `cat $PGDATA/PG_VERSION` != $PGVERSION ]
  88. then
  89. echo
  90. echo $"An old version of the database format was found.\nYou need to upgrade the data format before using PostgreSQL.\nSee @docdir@/README.rpm-dist for more information."
  91. exit 1
  92. # This doesn't seem to do anything useful...
  93. # else
  94. # if echo "$TYPESET"|grep "declare -f success ()" >/dev/null
  95. # then
  96. # success "$PSQL_CHECK"
  97. # else
  98. # echo " [ OK ]"
  99. # fi
  100. # echo
  101. fi
  102. # No existing PGDATA! Initdb it.
  103. else
  104. echo -n $"Initializing database: "
  105. if [ ! -d $PGDATA ]
  106. then
  107. mkdir -p $PGDATA
  108. chown postgres.postgres $PGDATA
  109. chmod go-rwx $PGDATA
  110. fi
  111. # Make sure the locale from the initdb is preserved for later startups...
  112. [ -f /etc/sysconfig/i18n ] && cp /etc/sysconfig/i18n $PGDATA/../initdb.i18n
  113. # Just in case no locale was set, use en_US
  114. [ ! -f /etc/sysconfig/i18n ] && echo "LANG=en_US" > $PGDATA/../initdb.i18n
  115. # Is expanded this early to be used in the command su runs
  116. echo "export LANG LC_ALL LC_CTYPE LC_COLLATE LC_NUMERIC LC_CTYPE LC_TIME" >> $PGDATA/../initdb.i18n
  117. # Initialize the database
  118. # su -l postgres -s /bin/sh -c "/usr/bin/initdb --pgdata=$PGDATA -E EUC_JP --no-locale > /dev/null 2>&1" < /dev/null
  119. # now no need to specify the locale with no-locale option
  120. su -l postgres -s /bin/sh -c "/usr/bin/initdb --pgdata=$PGDATA > /dev/null 2>&1" < /dev/null
  121. [ -f $PGDATA/PG_VERSION ] && echo_success
  122. [ ! -f $PGDATA/PG_VERSION ] && echo_failure
  123. echo
  124. fi
  125. # Check for postmaster already running...
  126. # note that pg_ctl only looks at the data structures in PGDATA
  127. # you really do need the pidof()
  128. pid=`pidof -s /usr/bin/postmaster`
  129. if [ $pid ] && /usr/bin/pg_ctl status -D $PGDATA > /dev/null 2>&1
  130. then
  131. echo $"Postmaster already running."
  132. else
  133. #all systems go -- remove any stale lock files
  134. rm -f /tmp/.s.PGSQL.${PGPORT} > /dev/null
  135. echo -n "$PSQL_START"
  136. su -l -s /bin/sh -c "/usr/bin/pg_ctl -D $PGDATA -p /usr/bin/postmaster -o '-p ${PGPORT}' start > /dev/null 2>&1" postgres < /dev/null
  137. sleep 1
  138. pid=`pidof -s /usr/bin/postmaster`
  139. if [ $pid ]
  140. then
  141. if echo "$TYPESET"|grep "declare -f success ()" >/dev/null
  142. then
  143. success "$PSQL_START"
  144. else
  145. echo_success
  146. fi
  147. touch /var/lock/subsys/${NAME}
  148. echo $pid > /var/run/postmaster.pid
  149. echo
  150. else
  151. if echo "$TYPESET"|grep "declare -f failure ()" >/dev/null
  152. then
  153. failure "$PSQL_START"
  154. else
  155. echo_failure
  156. fi
  157. echo
  158. fi
  159. fi
  160. }
  161. stop(){
  162. echo -n $"Stopping ${NAME} service: "
  163. su -l -s /bin/sh -c "/usr/bin/pg_ctl stop -D $PGDATA -s -m fast" postgres > /dev/null 2>&1
  164. ret=$?
  165. if [ $ret -eq 0 ]
  166. then
  167. echo_success
  168. else
  169. echo_failure
  170. fi
  171. echo
  172. rm -f /var/run/postmaster.pid
  173. rm -f /var/lock/subsys/${NAME}
  174. }
  175. restart(){
  176. stop
  177. start
  178. }
  179. condrestart(){
  180. [ -e /var/lock/subsys/${NAME} ] && restart
  181. }
  182. reload(){
  183. su -l postgres -s /bin/sh -c "/usr/bin/pg_ctl reload -D $PGDATA -s" > /dev/null 2>&1
  184. }
  185. # This script is slightly unusual in that the name of the daemon (postmaster)
  186. # is not the same as the name of the subsystem (postgresql)
  187. # See how we were called.
  188. case "$1" in
  189. start)
  190. start
  191. ;;
  192. stop)
  193. stop
  194. ;;
  195. status)
  196. status postmaster
  197. ;;
  198. restart)
  199. restart
  200. ;;
  201. condrestart)
  202. condrestart
  203. ;;
  204. reload|force-reload)
  205. reload
  206. ;;
  207. *)
  208. echo $"Usage: $0 {start|stop|status|restart|condrestart|reload|force-reload}"
  209. exit 1
  210. esac
  211. exit 0