xref: /OK3568_Linux_fs/yocto/meta-openembedded/meta-oe/recipes-dbs/postgresql/files/postgresql.init (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#!/bin/sh
2*4882a593Smuzhiyun#
3*4882a593Smuzhiyun# postgresql	This is the init script for starting up the PostgreSQL
4*4882a593Smuzhiyun#		server.
5*4882a593Smuzhiyun#
6*4882a593Smuzhiyun# chkconfig: - 64 36
7*4882a593Smuzhiyun# description: PostgreSQL database server.
8*4882a593Smuzhiyun# processname: postmaster
9*4882a593Smuzhiyun# pidfile: /var/run/postmaster.PORT.pid
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun# This script is slightly unusual in that the name of the daemon (postmaster)
12*4882a593Smuzhiyun# is not the same as the name of the subsystem (postgresql)
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun# PGVERSION is the full package version, e.g., 8.4.0
15*4882a593Smuzhiyun# Note: the specfile inserts the correct value during package build
16*4882a593SmuzhiyunPGVERSION=9.2.4
17*4882a593Smuzhiyun# PGMAJORVERSION is major version, e.g., 10 (this should match PG_VERSION)
18*4882a593SmuzhiyunPGMAJORVERSION=`echo "$PGVERSION" | sed 's/^\([0-9]*\).*$/\1/'`
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun# Source function library.
21*4882a593Smuzhiyun. /etc/init.d/functions
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun# Find the name of the script
24*4882a593SmuzhiyunNAME=`basename $0`
25*4882a593Smuzhiyunif [ ${NAME:0:1} = "S" -o ${NAME:0:1} = "K" ]
26*4882a593Smuzhiyunthen
27*4882a593Smuzhiyun	NAME=${NAME:3}
28*4882a593Smuzhiyunfi
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun# For SELinux we need to use 'runuser' not 'su'
31*4882a593Smuzhiyunif [ -x /sbin/runuser ]
32*4882a593Smuzhiyunthen
33*4882a593Smuzhiyun    SU=runuser
34*4882a593Smuzhiyunelse
35*4882a593Smuzhiyun    SU=su
36*4882a593Smuzhiyunfi
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun# Set defaults for configuration variables
40*4882a593SmuzhiyunPGENGINE=/usr/bin
41*4882a593SmuzhiyunPGPORT=5432
42*4882a593SmuzhiyunPGDATA=/var/lib/postgresql/data
43*4882a593SmuzhiyunPGLOG=/var/lib/postgresql/pgstartup.log
44*4882a593Smuzhiyun# Value to set as postmaster process's oom_adj
45*4882a593SmuzhiyunPG_OOM_ADJ=-17
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun# Override defaults from /etc/sysconfig/postgresql if file is present
48*4882a593Smuzhiyun[ -f /etc/default/postgresql/${NAME} ] && . /etc/default/postgresql/${NAME}
49*4882a593Smuzhiyun
50*4882a593Smuzhiyunexport PGDATA
51*4882a593Smuzhiyunexport PGPORT
52*4882a593Smuzhiyun
53*4882a593Smuzhiyunlockfile="/var/lock/subsys/${NAME}"
54*4882a593Smuzhiyunpidfile="/var/run/postmaster.${PGPORT}.pid"
55*4882a593Smuzhiyun
56*4882a593Smuzhiyunscript_result=0
57*4882a593Smuzhiyun
58*4882a593Smuzhiyunstart(){
59*4882a593Smuzhiyun	[ -x "$PGENGINE/postmaster" ] || exit 5
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun	PSQL_START=$"Starting ${NAME} service: "
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun	# Make sure startup-time log file is valid
64*4882a593Smuzhiyun	if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ]
65*4882a593Smuzhiyun	then
66*4882a593Smuzhiyun		touch "$PGLOG" || exit 4
67*4882a593Smuzhiyun		chown postgres:postgres "$PGLOG"
68*4882a593Smuzhiyun		chmod go-rwx "$PGLOG"
69*4882a593Smuzhiyun		[ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
70*4882a593Smuzhiyun	fi
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun	# Check for the PGDATA structure
73*4882a593Smuzhiyun	if [ -f "$PGDATA/PG_VERSION" ] && [ -d "$PGDATA/base" ]
74*4882a593Smuzhiyun	then
75*4882a593Smuzhiyun		# Check version of existing PGDATA
76*4882a593Smuzhiyun		if [ x`cat "$PGDATA/PG_VERSION"` != x"$PGMAJORVERSION" ]
77*4882a593Smuzhiyun		then
78*4882a593Smuzhiyun			SYSDOCDIR="(Your System's documentation directory)"
79*4882a593Smuzhiyun			if [ -d "/usr/doc/postgresql-$PGVERSION" ]
80*4882a593Smuzhiyun			then
81*4882a593Smuzhiyun				SYSDOCDIR=/usr/doc
82*4882a593Smuzhiyun			fi
83*4882a593Smuzhiyun			if [ -d "/usr/share/doc/postgresql-$PGVERSION" ]
84*4882a593Smuzhiyun			then
85*4882a593Smuzhiyun				SYSDOCDIR=/usr/share/doc
86*4882a593Smuzhiyun			fi
87*4882a593Smuzhiyun			if [ -d "/usr/doc/packages/postgresql-$PGVERSION" ]
88*4882a593Smuzhiyun			then
89*4882a593Smuzhiyun				SYSDOCDIR=/usr/doc/packages
90*4882a593Smuzhiyun			fi
91*4882a593Smuzhiyun			if [ -d "/usr/share/doc/packages/postgresql-$PGVERSION" ]
92*4882a593Smuzhiyun			then
93*4882a593Smuzhiyun				SYSDOCDIR=/usr/share/doc/packages
94*4882a593Smuzhiyun			fi
95*4882a593Smuzhiyun			echo
96*4882a593Smuzhiyun			echo $"An old version of the database format was found."
97*4882a593Smuzhiyun			echo $"You need to upgrade the data format before using PostgreSQL."
98*4882a593Smuzhiyun			echo $"See $SYSDOCDIR/postgresql-$PGVERSION/README.rpm-dist for more information."
99*4882a593Smuzhiyun			exit 1
100*4882a593Smuzhiyun		fi
101*4882a593Smuzhiyun	else
102*4882a593Smuzhiyun		# No existing PGDATA! Warn the user to initdb it.
103*4882a593Smuzhiyun		echo
104*4882a593Smuzhiyun                echo "$PGDATA is missing. Use \"postgresql-setup initdb\" to initialize the cluster first."
105*4882a593Smuzhiyun		echo -n " [FAILED] "
106*4882a593Smuzhiyun		echo
107*4882a593Smuzhiyun		exit 1
108*4882a593Smuzhiyun	fi
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun	echo -n "$PSQL_START"
111*4882a593Smuzhiyun	test x"$PG_OOM_ADJ" != x && echo "$PG_OOM_ADJ" > /proc/self/oom_score_adj
112*4882a593Smuzhiyun	$SU -l postgres -c "$PGENGINE/postmaster -p '$PGPORT' -D '$PGDATA' ${PGOPTS} &" >> "$PGLOG" 2>&1 < /dev/null
113*4882a593Smuzhiyun	sleep 2
114*4882a593Smuzhiyun	pid=`head -n 1 "$PGDATA/postmaster.pid" 2>/dev/null`
115*4882a593Smuzhiyun	if [ "x$pid" != x ]
116*4882a593Smuzhiyun	then
117*4882a593Smuzhiyun		echo -n " [ OK ]"
118*4882a593Smuzhiyun		touch "$lockfile"
119*4882a593Smuzhiyun		echo $pid > "$pidfile"
120*4882a593Smuzhiyun		echo
121*4882a593Smuzhiyun	else
122*4882a593Smuzhiyun		echo -n  " [FAILED]"
123*4882a593Smuzhiyun		echo
124*4882a593Smuzhiyun		script_result=1
125*4882a593Smuzhiyun	fi
126*4882a593Smuzhiyun}
127*4882a593Smuzhiyun
128*4882a593Smuzhiyunstop(){
129*4882a593Smuzhiyun	echo -n $"Stopping ${NAME} service: "
130*4882a593Smuzhiyun	if [ -e "$lockfile" ]
131*4882a593Smuzhiyun	then
132*4882a593Smuzhiyun	    $SU -l postgres -c "$PGENGINE/pg_ctl stop -D '$PGDATA' -s -m fast" > /dev/null 2>&1 < /dev/null
133*4882a593Smuzhiyun	    ret=$?
134*4882a593Smuzhiyun	    if [ $ret -eq 0 ]
135*4882a593Smuzhiyun	    then
136*4882a593Smuzhiyun		echo -n " [ OK ] "
137*4882a593Smuzhiyun		rm -f "$pidfile"
138*4882a593Smuzhiyun		rm -f "$lockfile"
139*4882a593Smuzhiyun	    else
140*4882a593Smuzhiyun		echo -n " [FAILED] "
141*4882a593Smuzhiyun		script_result=1
142*4882a593Smuzhiyun	    fi
143*4882a593Smuzhiyun	else
144*4882a593Smuzhiyun	    # not running; per LSB standards this is "ok"
145*4882a593Smuzhiyun	    echo -n " [ OK ] "
146*4882a593Smuzhiyun	fi
147*4882a593Smuzhiyun	echo
148*4882a593Smuzhiyun}
149*4882a593Smuzhiyun
150*4882a593Smuzhiyunrestart(){
151*4882a593Smuzhiyun    stop
152*4882a593Smuzhiyun    start
153*4882a593Smuzhiyun}
154*4882a593Smuzhiyun
155*4882a593Smuzhiyuncondrestart(){
156*4882a593Smuzhiyun    [ -e "$lockfile" ] && restart || :
157*4882a593Smuzhiyun}
158*4882a593Smuzhiyun
159*4882a593Smuzhiyunreload(){
160*4882a593Smuzhiyun    $SU -l postgres -c "$PGENGINE/pg_ctl reload -D '$PGDATA' -s" > /dev/null 2>&1 < /dev/null
161*4882a593Smuzhiyun}
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun# See how we were called.
165*4882a593Smuzhiyuncase "$1" in
166*4882a593Smuzhiyun  start)
167*4882a593Smuzhiyun	start
168*4882a593Smuzhiyun	;;
169*4882a593Smuzhiyun  stop)
170*4882a593Smuzhiyun	stop
171*4882a593Smuzhiyun	;;
172*4882a593Smuzhiyun  status)
173*4882a593Smuzhiyun	status postmaster
174*4882a593Smuzhiyun	script_result=$?
175*4882a593Smuzhiyun	;;
176*4882a593Smuzhiyun  restart)
177*4882a593Smuzhiyun	restart
178*4882a593Smuzhiyun	;;
179*4882a593Smuzhiyun  condrestart|try-restart)
180*4882a593Smuzhiyun	condrestart
181*4882a593Smuzhiyun	;;
182*4882a593Smuzhiyun  reload)
183*4882a593Smuzhiyun	reload
184*4882a593Smuzhiyun	;;
185*4882a593Smuzhiyun  force-reload)
186*4882a593Smuzhiyun	restart
187*4882a593Smuzhiyun	;;
188*4882a593Smuzhiyun  *)
189*4882a593Smuzhiyun	echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
190*4882a593Smuzhiyun	exit 2
191*4882a593Smuzhiyunesac
192*4882a593Smuzhiyun
193*4882a593Smuzhiyunexit $script_result
194