source: projects/specs/trunk/p/postgresql/postgresql.init @ 9618

Revision 9618, 6.7 KB checked in by tomop, 9 years ago (diff)

postgresql-9.4.4-2

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