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

Revision 7298, 6.9 KB checked in by iwaim, 11 years ago (diff)

postgresql-9.0.7-1: init script

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