postgresql: move initdb to postgresql-setup

We shouldn't use sysvinit init script to initialize database when use systemd
as the init manager, so split initdb function to postgresql-setup.
Before starting postgresql server, we can use "postgresql-setup initdb" to
initialize the database cluster.

Signed-off-by: Chong Lu <Chong.Lu@windriver.com>
Signed-off-by: Martin Jansa <Martin.Jansa@gmail.com>
This commit is contained in:
Chong.Lu@windriver.com
2014-09-16 17:36:35 +08:00
committed by Martin Jansa
parent cf2ab9ecbf
commit dfa0b4c86d
3 changed files with 77 additions and 50 deletions

View File

@@ -0,0 +1,73 @@
#!/bin/sh
#
# postgresql-setup Initialization operation for PostgreSQL
# For SELinux we need to use 'runuser' not 'su'
if [ -x /sbin/runuser ]
then
SU=runuser
else
SU=su
fi
PGENGINE=/usr/bin
PGDATA=/var/lib/postgresql/data
PGLOG=/var/lib/postgresql/pgstartup.log
script_result=0
initdb(){
if [ -f "$PGDATA/PG_VERSION" ]
then
echo -n "Data directory is not empty!"
echo -n " [FAILED] "
echo
script_result=1
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"
fi
# Clean up SELinux tagging for PGDATA
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGDATA"
# Make sure the startup-time log file is OK, too
if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ]
then
touch "$PGLOG" || exit 1
chown postgres:postgres "$PGLOG"
chmod go-rwx "$PGLOG"
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
fi
# Initialize the database
$SU -l postgres -c "$PGENGINE/initdb --pgdata='$PGDATA' --auth='ident'" >> "$PGLOG" 2>&1 < /dev/null
# Create directory for postmaster log
mkdir "$PGDATA/pg_log"
chown postgres:postgres "$PGDATA/pg_log"
chmod go-rwx "$PGDATA/pg_log"
if [ -f "$PGDATA/PG_VERSION" ]
then
echo -n " [ OK ] "
else
echo -n " [FAILED] "
script_result=1
fi
echo
fi
}
case "$1" in
initdb)
initdb
;;
*)
echo "Usage: $0 initdb"
exit 2
esac
exit $script_result

View File

@@ -101,7 +101,7 @@ start(){
else
# No existing PGDATA! Warn the user to initdb it.
echo
echo "$PGDATA is missing. Use \"service postgresql initdb\" to initialize the cluster first."
echo "$PGDATA is missing. Use \"postgresql-setup initdb\" to initialize the cluster first."
echo -n " [FAILED] "
echo
exit 1
@@ -160,51 +160,6 @@ reload(){
$SU -l postgres -c "$PGENGINE/pg_ctl reload -D '$PGDATA' -s" > /dev/null 2>&1 < /dev/null
}
initdb(){
if [ -f "$PGDATA/PG_VERSION" ]
then
echo -n "Data directory is not empty!"
echo -n " [FAILED] "
echo
script_result=1
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"
fi
# Clean up SELinux tagging for PGDATA
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGDATA"
# Make sure the startup-time log file is OK, too
if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ]
then
touch "$PGLOG" || exit 1
chown postgres:postgres "$PGLOG"
chmod go-rwx "$PGLOG"
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
fi
# Initialize the database
$SU -l postgres -c "$PGENGINE/initdb --pgdata='$PGDATA' --auth='ident'" >> "$PGLOG" 2>&1 < /dev/null
# Create directory for postmaster log
mkdir "$PGDATA/pg_log"
chown postgres:postgres "$PGDATA/pg_log"
chmod go-rwx "$PGDATA/pg_log"
if [ -f "$PGDATA/PG_VERSION" ]
then
echo -n " [ OK ] "
else
echo -n " [FAILED] "
script_result=1
fi
echo
fi
}
# See how we were called.
case "$1" in
@@ -230,11 +185,8 @@ case "$1" in
force-reload)
restart
;;
initdb)
initdb
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|initdb}"
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
esac

View File

@@ -29,6 +29,7 @@ SRC_URI = "http://ftp.postgresql.org/pub/source/v${PV}/${BP}.tar.bz2 \
file://postgresql-bashprofile \
file://postgresql.pam \
file://0001-Use-pkg-config-for-libxml2-detection.patch \
file://postgresql-setup \
"
LEAD_SONAME = "libpq.so"
@@ -171,6 +172,7 @@ do_install_append() {
install -d ${D}${sysconfdir}/init.d
install -m 0755 ${WORKDIR}/${BPN}.init ${D}${sysconfdir}/init.d/${BPN}-server
sed -i -e "s/^PGVERSION=.*$/PGVERSION=${PV}/g" ${D}${sysconfdir}/init.d/${BPN}-server
install -m 0755 ${WORKDIR}/${BPN}-setup ${D}${bindir}/${BPN}-setup
install -d -m 700 ${D}${localstatedir}/lib/${BPN}/data
install -d -m 700 ${D}${localstatedir}/lib/${BPN}/backups
install -m 644 ${WORKDIR}/${BPN}-bashprofile ${D}${localstatedir}/lib/${BPN}/.bash_profile