summaryrefslogtreecommitdiffstats
path: root/system/postgresql/rc.postgresql.new
blob: 7e984c65820589adf91794468ddeb990ebba661c (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
#!/bin/bash

## PostgreSQL startup script for Slackware Linux
##
## Copyright (c) 2007-2013 Adis Nezirovic <adis _at_ linux.org.ba>
## Licensed under GNU GPL v2

# Do not source this script (since it contains exit() calls)

# Before you can run postgresql you'll need to create the
# database files in /var/lib/pgsql. The following should do
# the trick.
#
#   $ su postgres -c "initdb -D /var/lib/pgsql/@PG_VERSION@/data"
#

# Since PostgreSQL 9.3 this startup script can run multiple PostgreSQL
# versions on different ports and with different data dirs.
# 
# e.g. PG_VERSION=9.4 PG_PORT=6432 /etc/rc.d/rc.postgresql start
# 
# Older PostgreSQL 9.x versions are supported too, just use PG_VERSION=9.2

PG_VERSION=${PG_VERSION:-@PG_VERSION@}
PG_PORT=${PG_PORT:-@PG_PORT@}
LIBDIRSUFFIX="@LIBDIRSUFFIX@"

if [ "x$PG_VERSION" != "x9.2" ];then
	LOGFILE=/var/log/postgresql-$PG_VERSION
	DATADIR=/var/lib/pgsql/$PG_VERSION/data
	POSTGRES=/usr/lib${LIBDIRSUFFIX}/postgresql/$PG_VERSION/bin/postgres
	PG_CTL=/usr/lib${LIBDIRSUFFIX}/postgresql/$PG_VERSION/bin/pg_ctl
else
	LOGFILE=/var/log/postgresql
	DATADIR=/var/lib/pgsql/data
	POSTGRES=/usr/bin/postgres
	PG_CTL=/usr/bin/pg_ctl
fi

PIDFILE=postmaster.pid

# oom-killer score
# if defined and set to -1000, main postmaster wont be killed
# by Linux OOM killer, but individual backends still could be
# (since OOM_SCORE_ADJ in SlackBuild is set to 0)
#
# http://www.kernel.org/doc/Documentation/filesystems/proc.txt
OOM_SCORE_ADJ=-1000

# Return values (according to LSB):
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running

pg_ctl()
{
	CMD="$PG_CTL -o '-p $PG_PORT' $@"
	su - postgres -c "$CMD"
}

if [ ! -f $POSTGRES ]; then
	echo "Could not find 'postgres' binary. Maybe PostgreSQL is not installed properly?"
	exit 5
fi

case "$1" in

	"start")
		echo "Starting PostgreSQL"
		touch $LOGFILE
		chown postgres:wheel $LOGFILE
		chmod 0640 $LOGFILE
	
		if [ ! -e $DATADIR/PG_VERSION ]; then
			echo "You should initialize the PostgreSQL database at location $DATADIR"
			exit 6
		fi
	
		if [ $(pgrep -f $POSTGRES) ]; then

			echo "PostgreSQL daemon already running"
			if [ ! -f $DATADIR/$PIDFILE ]; then
				echo "Warning: Missing pid file $DATADIR/$PIDFILE"
			fi
			exit 1

		else # remove old socket, if it exists and no daemon is running.

			if [ ! -f $DATADIR/$PIDFILE ]; then
				rm -f /tmp/.s.PGSQL.$PG_PORT
				rm -f /tmp/.s.PGSQL.$PG_PORT.lock
				test x"$OOM_SCORE_ADJ" != x && echo "$OOM_SCORE_ADJ" > /proc/self/oom_score_adj
				pg_ctl start -w -l $LOGFILE -D $DATADIR
				exit 0
			else
				echo "PostgreSQL daemon was not properly shut down"
				echo "Please remove stale pid file $DATADIR/$PIDFILE"
				exit 7
			fi

		fi	
	;;

	"stop")
		echo "Shutting down PostgreSQL..."
		pg_ctl stop -l $LOGFILE -D $DATADIR -m smart
	;;

	"force-stop")
		# Take care! This will kill _all_ client connections
		# and rollback current transactions.
		echo "Shutting down PostgreSQL (fast)..."
		pg_ctl stop -l $LOGFILE -D $DATADIR -m fast
	;;
	
	"unclean-stop")
		# Take care! This will abort server process itself
		# resulting with database recovery on next start.
		echo "Shutting down PostgreSQL (immediate)..."
		pg_ctl stop -l $LOGFILE -D $DATADIR -m immediate
	;;

	"restart")
		echo "Restarting PostgreSQL..."
		test x"$OOM_SCORE_ADJ" != x && echo "$OOM_SCORE_ADJ" > /proc/self/oom_score_adj
		pg_ctl restart -l $LOGFILE -D $DATADIR -m smart
	;;

	"force-restart")
		# Take care! This will kill _all_ client connections
		# and rollback current transactions.
		echo "Restarting PostgreSQL (fast)..."
		pg_ctl restart -l $LOGFILE -D $DATADIR -m fast
	;;

	"unclean-restart")
		# Take care: This will abort server process itself
		# resulting with database recovery on start.
		echo "Restarting PostgreSQL (immediate)..."
		pg_ctl restart -l $LOGFILE -D $DATADIR -m immediate
	;;

	"reload")
		echo "Reloading configuration for PostgreSQL..."
		pg_ctl reload -l $LOGFILE -D $DATADIR -m smart
	;;

	"status")
		if [ $(pgrep -f $POSTGRES) ]; then
			echo "PostgreSQL is running"

			if [ ! -e $DATADIR/$PIDFILE ]; then
				echo "Warning: Missing pid file $DATADIR/$PIDFILE"
			fi

			exit 0
		else
			echo "PostgreSQL is stopped"

			if [ -e $DATADIR/$PIDFILE ]; then
				echo "Detected stale pid file $DATADIR/$PIDFILE"
			fi

			exit 0
		fi
	;;

	*)
		# unclean-stop and unclean-restart are not documented on purpose.
		echo "Usage: $0 {start|stop|force-stop|status|restart|force-restart|reload}"
		exit 1
	;;
esac