postgresql.init 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. # PGVERSION must be replaced real version
  50. # In vine new postgresql.spec, version must be replaced by building
  51. PGVERSION=9.0
  52. # Source function library.
  53. INITD=/etc/rc.d/init.d
  54. . $INITD/functions
  55. # Get function listing for cross-distribution logic.
  56. TYPESET=`typeset -f|grep "declare"`
  57. # Get config.
  58. . /etc/sysconfig/network
  59. # Find the name of the script
  60. NAME=`basename $0`
  61. # Set defaults for port and database directory
  62. PGPORT=5432
  63. export PGDATA=/var/lib/pgsql
  64. if [ -f $PGDATA/PG_VERSION ] && [ -d $PGDATA/base/template1 ]
  65. then
  66. echo "Using old-style directory structure"
  67. else
  68. export PGDATA=/var/lib/pgsql/data
  69. fi
  70. # Override defaults from /etc/sysconfig/pgsql if file is present
  71. [ -f /etc/sysconfig/pgsql/${NAME} ] && . /etc/sysconfig/pgsql/${NAME}
  72. export PGDATA
  73. export PGPORT
  74. export PGOPTS
  75. # Check that networking is up.
  76. # Pretty much need it for postmaster.
  77. [ "${NETWORKING}" = "no" ] && exit 0
  78. [ -f /usr/bin/postmaster ] || exit 0
  79. start(){
  80. PSQL_START=$"Starting ${NAME} service: "
  81. # Check for the PGDATA structure
  82. if [ -f $PGDATA/PG_VERSION ] && [ -d $PGDATA/base ]
  83. then
  84. # Check version of existing PGDATA
  85. if [ `cat $PGDATA/PG_VERSION` != $PGVERSION ]
  86. then
  87. SYSDOCDIR="(Your System's documentation directory)"
  88. if [ -d /usr/doc/postgresql-$PGVERSION ]
  89. then
  90. SYSDOCDIR=/usr/doc
  91. fi
  92. if [ -d /usr/share/doc/postgresql-$PGVERSION ]
  93. then
  94. SYSDOCDIR=/usr/share/doc
  95. fi
  96. if [ -d /usr/doc/packages/postgresql-$PGVERSION ]
  97. then
  98. SYSDOCDIR=/usr/doc/packages
  99. fi
  100. if [ -d /usr/share/doc/packages/postgresql-$PGVERSION ]
  101. then
  102. SYSDOCDIR=/usr/share/doc/packages
  103. fi
  104. echo
  105. echo $"An old version of the database format was found.\nYou need to upgrade the data format before using PostgreSQL.\nSee $SYSDOCDIR/postgresql-$PGVERSION/README.rpm-dist for more information."
  106. exit 1
  107. # This doesn't seem to do anything useful...
  108. # else
  109. # if echo "$TYPESET"|grep "declare -f success ()" >/dev/null
  110. # then
  111. # success "$PSQL_CHECK"
  112. # else
  113. # echo " [ OK ]"
  114. # fi
  115. # echo
  116. fi
  117. # No existing PGDATA! Initdb it.
  118. else
  119. echo -n $"Initializing database: "
  120. if [ ! -d $PGDATA ]
  121. then
  122. mkdir -p $PGDATA
  123. chown postgres.postgres $PGDATA
  124. chmod go-rwx $PGDATA
  125. fi
  126. # Make sure the locale from the initdb is preserved for later startups...
  127. [ -f /etc/sysconfig/i18n ] && cp /etc/sysconfig/i18n $PGDATA/../initdb.i18n
  128. # Just in case no locale was set, use en_US
  129. [ ! -f /etc/sysconfig/i18n ] && echo "LANG=en_US" > $PGDATA/../initdb.i18n
  130. # Is expanded this early to be used in the command su runs
  131. echo "export LANG LC_ALL LC_CTYPE LC_COLLATE LC_NUMERIC LC_CTYPE LC_TIME" >> $PGDATA/../initdb.i18n
  132. # Initialize the database
  133. # su -l postgres -s /bin/sh -c "/usr/bin/initdb --pgdata=$PGDATA -E EUC_JP --no-locale > /dev/null 2>&1" < /dev/null
  134. # now no need to specify the locale with no-locale option
  135. su -l postgres -s /bin/sh -c "/usr/bin/initdb --pgdata=$PGDATA > /dev/null 2>&1" < /dev/null
  136. [ -f $PGDATA/PG_VERSION ] && echo_success
  137. [ ! -f $PGDATA/PG_VERSION ] && echo_failure
  138. echo
  139. fi
  140. # Check for postmaster already running...
  141. # note that pg_ctl only looks at the data structures in PGDATA
  142. # you really do need the pidof()
  143. pid=`pidof -s /usr/bin/postmaster`
  144. if [ $pid ] && /usr/bin/pg_ctl status -D $PGDATA > /dev/null 2>&1
  145. then
  146. echo $"Postmaster already running."
  147. else
  148. #all systems go -- remove any stale lock files
  149. rm -f /tmp/.s.PGSQL.${PGPORT} > /dev/null
  150. echo -n "$PSQL_START"
  151. 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
  152. sleep 1
  153. pid=`pidof -s /usr/bin/postmaster`
  154. if [ $pid ]
  155. then
  156. if echo "$TYPESET"|grep "declare -f success ()" >/dev/null
  157. then
  158. success "$PSQL_START"
  159. else
  160. echo_success
  161. fi
  162. touch /var/lock/subsys/${NAME}
  163. echo $pid > /var/run/postmaster.pid
  164. echo
  165. else
  166. if echo "$TYPESET"|grep "declare -f failure ()" >/dev/null
  167. then
  168. failure "$PSQL_START"
  169. else
  170. echo_failure
  171. fi
  172. echo
  173. fi
  174. fi
  175. }
  176. stop(){
  177. echo -n $"Stopping ${NAME} service: "
  178. su -l -s /bin/sh -c "/usr/bin/pg_ctl stop -D $PGDATA -s -m fast" postgres > /dev/null 2>&1
  179. ret=$?
  180. if [ $ret -eq 0 ]
  181. then
  182. echo_success
  183. else
  184. echo_failure
  185. fi
  186. echo
  187. rm -f /var/run/postmaster.pid
  188. rm -f /var/lock/subsys/${NAME}
  189. }
  190. restart(){
  191. stop
  192. start
  193. }
  194. condrestart(){
  195. [ -e /var/lock/subsys/${NAME} ] && restart
  196. }
  197. reload(){
  198. su -l postgres -s /bin/sh -c "/usr/bin/pg_ctl reload -D $PGDATA -s" > /dev/null 2>&1
  199. }
  200. # This script is slightly unusual in that the name of the daemon (postmaster)
  201. # is not the same as the name of the subsystem (postgresql)
  202. # See how we were called.
  203. case "$1" in
  204. start)
  205. start
  206. ;;
  207. stop)
  208. stop
  209. ;;
  210. status)
  211. status postmaster
  212. ;;
  213. restart)
  214. restart
  215. ;;
  216. condrestart)
  217. condrestart
  218. ;;
  219. reload|force-reload)
  220. reload
  221. ;;
  222. *)
  223. echo $"Usage: $0 {start|stop|status|restart|condrestart|reload|force-reload}"
  224. exit 1
  225. esac
  226. exit 0