postgresql.init 6.9 KB

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