summaryrefslogtreecommitdiff
path: root/user/postgresql/postgresql.initd
blob: 1c1a5dcf9408371d8bc989e95192cfcc9e012094 (plain) (blame)
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
#!/sbin/openrc-run

extra_started_commands="reload"
description_reload="Reload configuration"

extra_stopped_commands="setup"
description_setup="Initialize a new PostgreSQL cluster"

# Note: Uppercase variables are here for backward compatibility.

: ${user:=${PGUSER:-"postgres"}}
: ${group:=${PGGROUP:-"postgres"}}

: ${auto_setup:=${AUTO_SETUP:-"yes"}}
: ${start_timeout:=${START_TIMEOUT:-10}}
: ${nice_timeout:=${NICE_TIMEOUT:-60}}
: ${rude_quit:=${RUDE_QUIT:-"yes"}}
: ${rude_timeout:=${RUDE_TIMEOUT:-30}}
: ${force_quit:=${FORCE_QUIT:-"no"}}
: ${force_timeout:=${FORCE_TIMEOUT:-2}}

: ${data_dir:=${PGDATA:-"/var/lib/postgresql/@VERSION@/data"}}
: ${conf_dir:=$data_dir}
: ${env_vars:=${PG_EXTRA_ENV:-}}
: ${initdb_opts:=${PG_INITDB_OPTS:-}}
: ${logfile:="$data_dir/postmaster.log"}
: ${pg_opts:=${PGOPTS:-}}
: ${port:=${PGPORT:-5432}}

command="/usr/bin/postgres"

conffile="$conf_dir/postgresql.conf"
pidfile="$data_dir/postmaster.pid"
start_stop_daemon_args="
	--user $user
	--group $group
	--pidfile $pidfile
	--wait 100"

depend() {
	use net
	after firewall

	if [ "$(get_config log_destination)" = "syslog" ]; then
		use logger
	fi
}

start_pre() {
	check_deprecated_var WAIT_FOR_START start_timeout
	check_deprecated_var WAIT_FOR_DISCONNECT nice_timeout
	check_deprecated_var WAIT_FOR_CLEANUP rude_timeout
	check_deprecated_var WAIT_FOR_QUIT force_timeout

	if [ ! -d "$data_dir/base" ]; then
		if yesno "$auto_setup"; then
			setup || return 1
		else
			eerror "Database not found at: $data_dir"
			eerror "Please make sure that 'data_dir' points to the right path."
			eerror "You can run '/etc/init.d/postgresql setup' to setup a new database cluster."
			return 1
		fi
	fi

	local socket_dirs="$(get_config "unix_socket_directories" "/run/postgresql")"
	local port="$(get_config "port" "$port")"

	start_stop_daemon_args="$start_stop_daemon_args --env PGPORT=$port"

	(
		# Set the proper permission for the socket paths and create them if
		# then don't exist.
		set -f; IFS=","
		for dir in $socket_dirs; do
			if [ -e "${dir%/}/.s.PGSQL.$port" ]; then
				eerror "Socket conflict. A server is already listening on:"
				eerror "    ${dir%/}/.s.PGSQL.$port"
				eerror "Hint: Change 'port' to listen on a different socket."
				return 1
			elif [ "${dir%/}" != "/tmp" ]; then
				checkpath -d -m 1775 -o $user:$group "$dir"
			fi
		done
	)
}

start() {
	local retval

	ebegin "Starting PostgreSQL"

	local var; for var in $env_vars; do
		start_stop_daemon_args="$start_stop_daemon_args --env $var"
	done

	rm -f "$pidfile"
	start-stop-daemon --start \
		$start_stop_daemon_args \
		--exec /usr/bin/pg_ctl \
		-- start \
			--silent \
			-w --timeout="$start_timeout" \
			--log="$logfile" \
			--pgdata="$conf_dir" \
			-o "--data-directory=$data_dir $pg_opts"
	retval=$?

	if [ $retval -ne 0 ]; then
		eerror "Check the log for a possible explanation of the above error:"
		eerror "    $logfile"
	fi
	eend $retval
}

stop() {
	local retry="SIGTERM/$nice_timeout"

	yesno "$rude_quit" \
		&& retry="$retry/SIGINT/$rude_timeout" \
		|| rude_timeout=0

	yesno "$force_quit" \
		&& retry="$retry/SIGQUIT/$force_timeout" \
		|| force_timeout=0

	local seconds="$(( $nice_timeout + $rude_timeout + $force_timeout ))"

	ebegin "Stopping PostgreSQL (this can take up to $seconds seconds)"

	start-stop-daemon --stop \
		--exec "$command" \
		--retry "$retry" \
		--progress \
		--pidfile "$pidfile"
	eend $?
}

reload() {
	ebegin "Reloading PostgreSQL configuration"

	start-stop-daemon --signal HUP --pidfile "$pidfile"
	eend $?
}

setup() {
	local bkpdir

	ebegin "Creating a new PostgreSQL database cluster"

	if [ -d "$data_dir/base" ]; then
		eend 1 "$data_dir/base already exists!"; return 1
	fi

	# If data_dir exists, backup configs.
	if [ -d "$data_dir" ]; then
		bkpdir="$(mktemp -d)"
		find "$data_dir" -type f -name "*.conf" -maxdepth 1 \
			-exec mv -v {} "$bkpdir"/ \;
		rm -rf "$data_dir"/*
	fi

	install -d -m 0700 -o $user -g $group "$data_dir"
	install -d -m 0750 -o $user -g $group "$conf_dir"

	cd "$data_dir"  # to avoid the: could not change directory to "/root"
	su $user -c "/usr/bin/initdb $initdb_opts --pgdata $data_dir"
	local retval=$?

	if [ -d "$bkpdir" ]; then
		# Move backuped configs back.
		mv -v "$bkpdir"/* "$data_dir"/
		rm -rf "$bkpdir"
	fi

	if [ "${data_dir%/}" != "${conf_dir%/}" ]; then
		# Move configs from data_dir to conf_dir and symlink them to data_dir.
		local name newname
		for name in postgresql.conf pg_hba.conf pg_ident.conf; do
			newname="$name"
			[ ! -e "$conf_dir"/$name ] || newname="$name.new"

			mv "$data_dir"/$name "$conf_dir"/$newname
			ln -s "$conf_dir"/$name "$data_dir"/$name
		done
	fi

	eend $retval
} 


get_config() {
	local name="$1"
	local default="${2:-}"

	if [ ! -f "$conffile" ]; then
		printf '%s\n' "$default"
		return 1
	fi
	sed -En "/^\s*${name}\b/{                      # find line starting with the name
		  s/^\s*${name}\s*=?\s*([^#]+).*/\1/;  # capture the value
		  s/\s*$//;                            # trim trailing whitespaces
		  s/^['\"](.*)['\"]$/\1/;              # remove delimiting quotes
		  p
		}" "$conffile" \
		| grep . || printf '%s\n' "$default"
}

check_deprecated_var() {
	local old_name="$1"
	local new_name="$2"

	if [ -n "$(getval "$old_name")" ]; then
		ewarn "Variable '$old_name' has been removed, please use '$new_name' instead."
	fi
}

getval() {
	eval "printf '%s\n' \"\$$1\""
}