summaryrefslogtreecommitdiffstats
path: root/postgresql.init
blob: 65c3ed40959022c1cef8c17bd71a3d00f93dde37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/bin/sh
# postgresql	This is the init script for starting up the PostgreSQL
#		server
#
# chkconfig: - 64 36
# description: Starts and stops the PostgreSQL backend daemon that handles \
#	       all database requests.
# processname: postmaster
# pidfile: /var/run/postmaster.pid

# Version 6.5.3-2 Lamar Owen
# Added code to determine if PGDATA exists, whether it is current version
#     or not, and initdb if no PGDATA (initdb will not overwrite a database).

# Version 7.0 Lamar Owen
# Added logging code
# Changed PGDATA.

# Version 7.0.2 Trond Eivind Glomsrd <teg@redhat.com>
# use functions, add conditional restart
          
# Version 7.0.3 Lamar Owen <lamar@postgresql.org>
# Check for the existence of functions before blindly using them 
# in particular -- check for success () and failure () before using.
# More Cross-distribution support -- PGVERSION variable, and docdir checks.

# Version 7.1 Release Candidate Lamar Owen <lamar@postgresql.org>
# initdb parameters have changed.

# Version 7.1.2 Trond Eivind Glomsrd <teg@redhat.com>
# Specify shell for su
# Handle stop better - kill unwanted output, make it wait until the database is ready
# Handle locales slightly differently - always using "C" isn't a valid option 
# Kill output from database initialization 
# Mark messages for translation

# Version 7.1.2-2.PGDG Lamar Owen <lamar.owen@wgcr.org>
# sync up.
# Karl's fixes for some quoting issues.

# Version 7.2b2 Lamar Owen <lamar.owen@wgcr.org>
# version change.

# Version 7.2 final.  Lamar Owen <lamar.owen@wgcr.org>
# reload from Peter E.
# Eliminate the pidof postmaster test in stop -- we're using pg_ctl so we don't need pidof.
# Tested the $? return for the stop script -- it does in fact propagate.
# TODO: multiple postmasters.

# Version 7.3 Lamar Owen <lamar.owen@ramifordistat.net>
# Multiple postmasters, courtesy Karl DeBisschop

# Version 7.4 Lamar Owen.

# Version 7.4.3 Tom Lane <tgl@sss.pgh.pa.us>
# Support condstop for uninstall
# Minor other changes suggested by Fernando Nasser.

# Version 7.4.5 Tom Lane <tgl@sss.pgh.pa.us>
# Rewrite to start postmaster directly, rather than via pg_ctl; this avoids
# fooling the postmaster's stale-lockfile check by having too many
# postgres-owned processes laying about.


# PGVERSION is:
PGVERSION=7.4

# Source function library.
INITD=/etc/rc.d/init.d
. $INITD/functions

# Get function listing for cross-distribution logic.
TYPESET=`typeset -f|grep "declare"`

# Get config.
. /etc/sysconfig/network

# Find the name of the script
NAME=`basename $0`
if [ ${NAME:0:1} = "S" -o ${NAME:0:1} = "K" ]
then
	NAME=${NAME:3}
fi

# For SELinux we need to use 'runuser' not 'su'
if [ -x /sbin/runuser ]
then
    SU=runuser
else
    SU=su
fi


# Set defaults for configuration variables
PGENGINE=/usr/bin
PGPORT=5432
export PGDATA=/var/lib/pgsql
if [ -f $PGDATA/PG_VERSION ] && [ -d $PGDATA/base/template1 ]
then
	echo "Using old-style directory structure"
else
	export PGDATA=/var/lib/pgsql/data
fi
PGLOG=/dev/null

# Override defaults from /etc/sysconfig/pgsql if file is present
[ -f /etc/sysconfig/pgsql/${NAME} ] && . /etc/sysconfig/pgsql/${NAME}
export PGDATA
export PGPORT
export PGOPTS

# Check that networking is up.
# Pretty much need it for postmaster.
[ "${NETWORKING}" = "no" ] && exit 0

[ -f $PGENGINE/postmaster ] || exit 0

start(){
	PSQL_START=$"Starting ${NAME} service: "

	# Check for the PGDATA structure
	if [ -f $PGDATA/PG_VERSION ] && [ -d $PGDATA/base ]
	then
	# Check version of existing PGDATA

		if [ `cat $PGDATA/PG_VERSION` != "$PGVERSION" ]
		then
			SYSDOCDIR="(Your System's documentation directory)"
			if [ -d /usr/doc/postgresql-$PGVERSION ]
			then
				SYSDOCDIR=/usr/doc
			fi
			if [ -d /usr/share/doc/postgresql-$PGVERSION ]
			then
				SYSDOCDIR=/usr/share/doc
			fi
			if [ -d /usr/doc/packages/postgresql-$PGVERSION ]
			then
				SYSDOCDIR=/usr/doc/packages
			fi
			if [ -d /usr/share/doc/packages/postgresql-$PGVERSION ]
			then
				SYSDOCDIR=/usr/share/doc/packages
			fi
			echo
			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."
			exit 1
#                       This doesn't seem to do anything useful...
#		else
#			if echo "$TYPESET"|grep "declare -f success ()" >/dev/null
#			then
#				success "$PSQL_CHECK"
#			else
#				echo "  [ OK ]"
#			fi
#			echo
		fi

	# No existing PGDATA! Initdb it.

	else
	        echo -n $"Initializing database: "
                if [ ! -e $PGDATA -a ! -h $PGDATA ]
		then
			mkdir -p $PGDATA || exit 1
			chown postgres:postgres $PGDATA
			chmod go-rwx $PGDATA
			[ -x /sbin/restorecon ] && restorecon -R $PGDATA 
		fi
		# Make sure the locale from the initdb is preserved for later startups...
		[ -f /etc/sysconfig/i18n ] && cp /etc/sysconfig/i18n $PGDATA/../initdb.i18n
		# Just in case no locale was set, use en_US
		[ ! -f /etc/sysconfig/i18n ] && echo "LANG=en_US" > $PGDATA/../initdb.i18n 
		# Is expanded this early to be used in the command $SU runs
		echo "export LANG LC_ALL LC_CTYPE LC_COLLATE LC_NUMERIC LC_CTYPE LC_TIME" >> $PGDATA/../initdb.i18n
		# Initialize the database
		$SU -l postgres -c "$PGENGINE/initdb --pgdata=$PGDATA" >> $PGLOG 2>&1 < /dev/null
		[ -f $PGDATA/PG_VERSION ] && echo_success
		[ ! -f $PGDATA/PG_VERSION ] && echo_failure
		echo
 	fi

	echo -n "$PSQL_START"
	$SU -l postgres -c "$PGENGINE/postmaster -p ${PGPORT} -D '${PGDATA}' ${PGOPTS} &" >> $PGLOG 2>&1 < /dev/null
	sleep 1
	pid=`pidof -s $PGENGINE/postmaster`
	if [ $pid ] && [ -f "${PGDATA}/postmaster.pid" ]
	then
		success "$PSQL_START"
		touch /var/lock/subsys/${NAME}
		head -n 1 "${PGDATA}/postmaster.pid" > /var/run/postmaster.${PGPORT}.pid
		echo
	else
		failure "$PSQL_START"
		echo
	fi
}

stop(){
	echo -n $"Stopping ${NAME} service: "
	$SU -l postgres -c "$PGENGINE/pg_ctl stop -D '${PGDATA}' -s -m fast" > /dev/null 2>&1 < /dev/null
	ret=$? 
	if [ $ret -eq 0 ]
	then
		echo_success
	else
		echo_failure
	fi
	echo
	rm -f /var/run/postmaster.${PGPORT}.pid
	rm -f /var/lock/subsys/${NAME}
}

restart(){
    stop
    start
}

condrestart(){
    [ -e /var/lock/subsys/${NAME} ] && restart
}

condstop(){
    [ -e /var/lock/subsys/${NAME} ] && stop
}

reload(){
    $SU -l postgres -c "$PGENGINE/pg_ctl reload -D '${PGDATA}' -s" > /dev/null 2>&1 < /dev/null
}

# This script is slightly unusual in that the name of the daemon (postmaster)
# is not the same as the name of the subsystem (postgresql)

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	status postmaster
	;;
  restart)
	restart
	;;
  condrestart)
	condrestart
	;;
  condstop)
	condstop
	;;
  reload|force-reload)
	reload
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|condrestart|condstop|reload|force-reload}"
	exit 1
esac

exit 0