summaryrefslogtreecommitdiffstats
path: root/main/postgresql/postgresql.initd
blob: 3452db85668678efabc441c920d4ad56ed38bf4e (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
#!/sbin/runscript
# Copyright 1999-2008 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/dev-db/postgresql-server/files/postgresql.init-8.3,v 1.4 2008/09/28 22:53:02 caleb Exp $
# Modifications to support Alpine Linux pg-restore

opts="${opts} reload setup"

depend() {
	use net
	after firewall
}

checkconfig() {
	[ -d "$PGDATA/base" ] && return 0
	echo "checking config"

	if [ -z "$AUTO_SETUP" ] ; then
		eerror "Database not found at: $PGDATA"
		eerror "Please make sure that PGDATA points to the right path."
		eerror "You can run '/etc/init.d/postgresql setup' to setup a new database cluster."
		return 1
	fi
	setup
}

start() {
	checkconfig || return 1

	ebegin "Starting PostgreSQL"

	if [ -f "$PGDATA/postmaster.pid" ] ; then
		rm -f "$PGDATA/postmaster.pid"
	fi

	local retval

	su -l ${PGUSER} \
		-c "env PGDATA=\"${PGDATA}\" /usr/bin/pg_ctl start ${WAIT_FOR_START} -o '--silent-mode=true ${PGOPTS}'" >/dev/null
	retval=$?
	[ $retval -ne 0 ] && eend $retval && return $retval

	# The following is to catch the case of an already running server
	# in which pg_ctl doesn't know to which server it connected to and false reports the server as 'up'
	sleep 2
	if [ ! -f "$PGDATA/postmaster.pid" ] ; then
		eerror "The pid-file doesn't exist but pg_ctl reported a running server."
		eerror "Please check whether there is another server running on the same port or read the log-file."
		eend 1
		return 1
	fi

	local pid
	read pid < "$PGDATA/postmaster.pid"
	test -d /proc/"${pid}" 
	eend $?
}

stop() {
	ebegin "Stopping PostgreSQL (this can take up to $(( ${WAIT_FOR_DISCONNECT} + ${WAIT_FOR_CLEANUP} )) seconds)"

	local retval

	su -l ${PGUSER} \
		-c "env PGDATA=\"${PGDATA}\" /usr/bin/pg_ctl stop -t ${WAIT_FOR_DISCONNECT} -m smart" >/dev/null

	retval=$?
	[ $retval -eq 0 ] && eend $retval && return $retval

	ewarn "Some clients did not disconnect within ${WAIT_FOR_DISCONNECT} seconds."
	ewarn "Going to shutdown the server anyway."

	su -l ${PGUSER} \
		-c "env PGDATA=\"${PGDATA}\" /usr/bin/pg_ctl stop -m fast" >/dev/null

	retval=$?
	[ $retval -eq 0 ] && eend $retval && return $retval

	if [ ${WAIT_FOR_QUIT} -eq 0 ] ; then
		eerror "Server did not shut down and sending the SIGQUIT has been disabled."
		eend $retval
		return $retval
	fi

	ewarn "Shutting down the server gracefully failed."
	ewarn "Forcing it to shutdown which leads to a recover-run on next startup."

	su -l ${PGUSER} \
		-c "env PGDATA=\"${PGDATA}\" /usr/bin/pg_ctl stop -m immediate" >/dev/null

	retval=$?
	[ $retval -eq 0 ] && eend $retval && return $retval

	eerror "Forced shutdown failed!!! Something is wrong with your system, please take care of it manually."
	eend $?
}

reload() {
	ebegin "Reloading PostgreSQL configuration"
	su -l ${PGUSER} \
		-c "env PGDATA=\"${PGDATA}\" /usr/bin/pg_ctl reload" >/dev/null
	eend $?
}

setup() { 
	ebegin "Creating a new PostgreSQL database cluster" 
	
	if [ -d "${PGDATA}/base" ] ; then 
		eend 1 "${PGDATA}/base already exists" 
		return 
	fi 

	mkdir -p "${PGDATA}"  2>/dev/null

	# If the pg_hba.conf and friends exist, move them
	local tmpdir="$( dirname "$PGDATA" )/tmp"
	mkdir -p "${tmpdir}" >/dev/null
	echo mv "${PGDATA}"/* "${tmpdir}" 
	mv "${PGDATA}"/* "${tmpdir}"  2>/dev/null

	rm -rf "${PGDATA}"/* 2>/dev/null
	chown -Rf postgres:postgres "${PGDATA}" 
	chmod 0700 "${PGDATA}" 
	cd "${PGDATA}" # to avoid the: could not change directory to "/root" 
	su -c "/usr/bin/initdb --pgdata ${PGDATA}" postgres
	einfo "You can use the '/etc/init.d/postgresql' script to run PostgreSQL instead"
	einfo "of 'pg_ctl'."
	local res=$?

	# move the pg_hba.conf and friends
	mv $tmpdir/* "$PGDATA" 2>/dev/null 
	rm -rf $tmpdir 2>/dev/null

	# Do not send a SIGHUP to postmaster;  its not necessary for a new database
	# and allows pg-restore to do a blind restore of an old database

	eend $res
}