1#!/bin/sh
2#
3# postgresql-setup      Initialization operation for PostgreSQL
4
5# For SELinux we need to use 'runuser' not 'su'
6if [ -x /sbin/runuser ]
7then
8    SU=runuser
9else
10    SU=su
11fi
12
13PGENGINE=/usr/bin
14PGDATA=/var/lib/postgresql/data
15PGLOG=/var/lib/postgresql/pgstartup.log
16script_result=0
17
18initdb(){
19    if [ -f "$PGDATA/PG_VERSION" ]
20    then
21	echo -n "Data directory is not empty!"
22	echo -n " [FAILED] "
23	echo
24	script_result=1
25    else
26	echo -n "Initializing database: "
27	if [ ! -e "$PGDATA" -a ! -h "$PGDATA" ]
28	then
29		mkdir -p "$PGDATA" || exit 1
30		chown postgres:postgres "$PGDATA"
31		chmod go-rwx "$PGDATA"
32	fi
33	# Clean up SELinux tagging for PGDATA
34	[ -x /sbin/restorecon ] && /sbin/restorecon "$PGDATA"
35
36	# Make sure the startup-time log file is OK, too
37	if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ]
38	then
39		touch "$PGLOG" || exit 1
40		chown postgres:postgres "$PGLOG"
41		chmod go-rwx "$PGLOG"
42		[ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
43	fi
44
45	# Initialize the database
46	$SU -l postgres -c "$PGENGINE/initdb --pgdata='$PGDATA' --auth='ident'" >> "$PGLOG" 2>&1 < /dev/null
47
48	# Create directory for postmaster log
49	mkdir "$PGDATA/pg_log"
50	chown postgres:postgres "$PGDATA/pg_log"
51	chmod go-rwx "$PGDATA/pg_log"
52
53	if [ -f "$PGDATA/PG_VERSION" ]
54	then
55	    echo -n " [ OK ] "
56	else
57	    echo -n " [FAILED] "
58	    script_result=1
59	fi
60	echo
61    fi
62}
63
64case "$1" in
65  initdb)
66	initdb
67	;;
68  *)
69	echo "Usage: $0 initdb"
70	exit 2
71esac
72
73exit $script_result
74