#!/bin/sh set -e # postgresql Debian package - post-installation script # guessdatestyle() { # Guess the postgresql datestyle to use for a given timezone (from glibc) # make sure TZ is set and exported TZ=`cat /etc/timezone` export TZ # find the timezone offset from UTC tzno=`date '+%z'` # Find the name of the zone - strip off unwanted header directories x=`echo $TZ | cut -d/ -f1` case $x in SystemV | posix | right ) x=`echo $TZ | cut -d/ -f2-4` ;; * ) x=$TZ ;; esac # What country does this zone belong to (if undefined, set it to __) tzcountry=`grep $x /usr/share/zoneinfo/zone.tab | head -1 | cut -c1-2` if [ -z "$tzcountry" ] then tzcountry=__ fi # Guess the timezone case $tzcountry in US ) # US date format GuessDateStyle=US ;; AR ) # Argentina GuessDateStyle=SQL ;; CA ) # Canada has no standard GuessDateStyle=ISO ;; DE ) # Germany has a style to itself GuessDateStyle=GERMAN ;; __ ) # No country, so see if the zone is a region or country name x=$TZ while echo $x | grep -q / do y=`dirname $x` case $y in SystemV | posix | right ) y=`basename $x` ;; esac x=$y done case $x in Africa | Canada | Indian | Mexico | America | Chile | GB | Iran | Mideast | Antarctica | Cuba | Israel | NZ | Singapore | Arctic | Jamaica | Asia | Japan | Navajo | Turkey | Atlantic | Kwajalein | Australia | Egypt | Libya | US | Brazil | Eire | Pacific | Hongkong | Poland | Europe | Iceland | Portugal) # try to guess the zone from the UTC offset and # the zone abbreviation if [ -z "$GuessDateStyle" ] then GuessDateStyle=EURO if [ "$LANG" = de_DE ] then GuessDateStyle=GERMAN elif [ $tzno -le -200 -a $tzno -gt -1200 ] then tzn=`date '+%Z'` case $tzn in ADT | AST | AKDT | AKST | CDT | CST | EDT | EST | HADT | HAST | HST | MDT | MST | NDT | PDT | PST) GuessDateStyle=US ;; esac fi fi ;; * ) # Not a country or region so use ISO GuessDateStyle=ISO ;; esac ;; * ) # The rest of the world uses normal European format GuessDateStyle=EURO ;; esac } # Put the ~/.profile in the postgres home directory # If there is not already a database, create one SHELL=/bin/sh TMPFILE=`mktemp /tmp/pg.XXXXXX` || exit 1 trap "rm -f ${TMPFILE}" 0 . /etc/postgresql/postmaster.init # If the preinst script set the flag file (that points PGDATA to # /var/postgres/data), read it and then delete it. if [ -f /var/lib/postgres/dpkg-postinst-flag ] then . /var/lib/postgres/dpkg-postinst-flag rm /var/lib/postgres/dpkg-postinst-flag cp /etc/postgresql/postmaster.init ${TMPFILE} sed "/^# POSTGRES_DATA=/s|^.*$|POSTGRES_DATA=${POSTGRES_DATA}|" <${TMPFILE} >/etc/postgresql/postmaster.init > ${TMPFILE} fi PGDATA=${POSTGRES_DATA:=/var/lib/postgres/data} PGBASE=/usr/lib/postgresql PGLIB=${PGBASE}/lib PATH=${PATH}:${PGBASE}/bin export PGDATA PGLIB PATH PGHOME=${POSTGRES_HOME:=`dirname ${PGDATA}`} if [ `dirname $PGHOME` = / ] then echo -n "In /etc/postgresql/postmaster.init, POSTGRES_HOME is configured as $PGHOME, which is unacceptable! (If POSTGRES_HOME is not defined, it is set to the parent directory of POSTGRES_DATA.) POSTGRES_HOME should be at least two levels below /, and POSTGRES_DATA should be at least 3 levels below /. Press return to continue " read x exit 1 fi if [ ! -d "${PGHOME}" -a ! -f "${PGHOME}" ] then echo Creating missing home directory ${PGHOME} for postgres install -m 700 -o postgres -g postgres -d ${PGHOME} fi if [ ! -d "${PGHOME}" ] then echo Cannot create home directory ${PGHOME} for postgres exit 1 fi # Make sure that /etc/passwd's home directory exists PSWDHOME=`grep 'postgres:' /etc/passwd | cut -f6 -d:` if [ ! -e ${PSWDHOME} ] then echo "The home directory given for postgres in /etc/passwd (${PSWDHOME})" echo does not exist echo "(Home directory from /etc/postgresql/postmaster.init is ${PGHOME})" exit 1 fi if [ ${PSWDHOME} != ${PGHOME} ] then echo Home directory for postgres in /etc/passwd is ${PSWDHOME} echo Home directory from /etc/postgresql/postmaster.init is ${PGHOME} echo -n Press the return key to continue read x fi PGSHELL=`grep -s '^postgres:' /etc/passwd | awk -F: '{print $7}'` if [ -z "${PGSHELL}" ] then PGSHELL=/bin/sh fi PGSHELL=`basename ${PGSHELL}` case ${PGSHELL} in bash) PROFILE=.bash_profile ;; sh | ksh) PROFILE=.profile ;; csh | tcsh) PROFILE=.login ;; *) PROFILE=.profile ;; esac install -m 700 -o postgres -g postgres -d ${PGDATA} grep -q -s postmaster.init ${PGHOME}/${PROFILE} || (echo Updating ${PGHOME}/${PROFILE} ... echo . /etc/postgresql/postmaster.init >>${PGHOME}/${PROFILE} if [ ${PROFILE} = .login ] then # C-shell syntax... echo setenv PATH /bin:/usr/bin:${PGBASE}/bin >>${PGHOME}/${PROFILE} echo setenv PGDATA \${POSTGRES_DATA:-/var/lib/postgres/data} >>${PGHOME}/${PROFILE} echo setenv PGLIB ${PGLIB} >>${PGHOME}/${PROFILE} else echo PATH=/bin:/usr/bin:${PGBASE}/bin >>${PGHOME}/${PROFILE} echo PGDATA=\${POSTGRES_DATA:-/var/lib/postgres/data} >>${PGHOME}/${PROFILE} echo PGLIB=${PGLIB} >>${PGHOME}/${PROFILE} echo export PGLIB PGDATA >>${PGHOME}/${PROFILE} fi) chown -R postgres.postgres ${PGDATA} ${PGHOME} /var/lib/postgres /var/run/postgresql chmod 755 /var/run/postgresql chmod 700 ${PGDATA} # Make sure we have the shared library (from libpgsql) if [ ! -f /usr/lib/libpq.so.2 ] then echo The shared library /usr/lib/libpq.so.2 is not present. Package echo libpgsql must be installed before we can continue. exit 1 fi if [ ! -f ${PGDATA}/PG_VERSION ] then echo "PostgreSQL databases can be created with any one of a number of different character encodings. Please choose the default encoding, which will be used for all newly-created databases in the absence of a specific encoding specification. The choices are: SQL_ASCII ASCII EUC_JP Japanese EUC EUC_CN Chinese EUC EUC_KR Korean EUC EUC_TW Taiwan EUC UNICODE Unicode(UTF-8) MULE_INTERNAL Mule internal LATIN1 ISO 8859-1 English and some European languages LATIN2 ISO 8859-2 English and some European languages LATIN3 ISO 8859-3 English and some European languages LATIN4 ISO 8859-4 English and some European languages LATIN5 ISO 8859-5 English and some European languages KOI8 KOI8-R WIN Windows CP1251 ALT Windows CP866 " ok= while [ -z "$ok" ] do echo -n "Enter default encoding (UNICODE): " read Encoding if [ -z "${Encoding}" ] then Encoding=UNICODE fi case ${Encoding} in SQL_ASCII | EUC_JP | EUC_CN | EUC_KR | EUC_TW | UNICODE | MULE_INTERNAL | LATIN1 | LATIN2 | LATIN3 | LATIN4 | LATIN5 | KOI8 | WIN | ALT ) ok=true ;; *) echo Invalid response, choose one of SQL_ASCII EUC_JP EUC_CN EUC_KR EUC_TW UNICODE MULE_INTERNAL LATIN1 LATIN2 LATIN3 LATIN4 LATIN5 KOI8 WIN ALT ;; esac done echo Now installing the PostgreSQL database files in ${PGDATA} echo su postgres -c "cd ${PGHOME}; . ./${PROFILE}; initdb -e ${Encoding} -l ${PGLIB} -r ${PGDATA} -u postgres" su postgres -c "cd ${PGHOME}; . ./${PROFILE}; initdb -e ${Encoding} -l ${PGLIB} -r ${PGDATA} -u postgres" echo PostgreSQL database now installed. echo Use /usr/bin/createdb to create a specific database and echo /usr/bin/createuser to enable other users to connect to a echo PostgreSQL database. echo echo In the first instance, these commands must be run by the echo user \'postgres\'. echo fi if [ "$1" = configure ] then if [ -z "$2" -o "$2" = "" ] then guessdatestyle cp /etc/postgresql/postmaster.init ${TMPFILE} echo "The date style affects how dates, times and timezone information is presented to the user; the backend has a default setting, though the user can override it in any session. These are the available date styles: Style Date Datetime ------------------------------------------------------- ISO 1999-07-17 1999-07-17 07:09:18+01 SQL 17/07/1999 17/07/1999 07:09:19.00 BST POSTGRES 17-07-1999 Sat 17 Jul 07:09:19 1999 BST GERMAN 17.07.1999 17.07.1999 07:09:19.00 BST NONEURO 07-17-1999 Sat Jul 17 07:09:19 1999 BST US 07-17-1999 Sat Jul 17 07:09:19 1999 BST EURO 17-07-1999 Sat 17 Jul 07:09:19 1999 BST" ok= while [ -z "$ok" ] do echo -n "Choose your default date style ($GuessDateStyle): " read ans if [ -z "$ans" ] then ans=$GuessDateStyle fi case $ans in ISO|SQL|POSTGRES|GERMAN|NONEURO|US|EURO) sed -e "/PGDATESTYLE/s/^.*$/PGDATESTYLE=$ans/" <${TMPFILE} \ >/etc/postgresql/postmaster.init ok=true ;; *) echo "Invalid response ($ans); answer must be one of ISO SQL POSTGRES GERMAN NONEURO US EURO" ;; esac done rm ${TMPFILE} fi fi # Make a symbolic link to the authorisation file if [ ! -L ${PGDATA}/pg_hba.conf ] then ln -sf /etc/postgresql/pg_hba.conf ${PGDATA}/pg_hba.conf fi # Make a symbolic link to the ident map if [ ! -L ${PGDATA}/pg_ident.conf ] then ln -sf /etc/postgresql/pg_ident.conf ${PGDATA}/pg_ident.conf fi echo echo The file /etc/postgresql/postgresql.env provides the normal set-up for echo an ordinary user running PostgreSQL. It is automatically read by the echo wrapper script for PostgreSQL user commands. echo # # We may need to tidy up /etc/crontab from previous violations of policy if grep -qs '^#-- postgresql begin *$' /etc/crontab then TMP=`mktemp /tmp/pg.XXXXXX` || exit 1 awk 'BEGIN {found=0} /^#-- postgresql begin *$/ {found = 1} /^#-- postgresql end *$/ {found = -1} {if (!found) print} {if (found == -1) found=0} END {if (found) exit 1}' /etc/crontab >$TMP && if [ -f $TMP ] then mv $TMP /etc/crontab fi fi # Make sure the log file exists with the correct ownership LOGFILE=${POSTGRES_LOG:-/var/log/postgres.log} touch $LOGFILE chown postgres.postgres $LOGFILE # # These bits would be added by debhelper if we didn't need to tweak the # last item update-rc.d postgresql defaults >/dev/null set +e /etc/init.d/postgresql restart || if [ $? -ne 255 ] then exit $? fi # Only do the next bit if the postmaster is running if ps --User postgres | grep -v grep | grep -q postmaster then # take a little nap while the postmaster sorts itself out sleep 5 # # set usecatupd to false for all users except superusers # set it to true for superusers (temporary expedient at 6.5.1-4) ( cd $PGHOME su postgres -c "psql -c \"update pg_shadow set usecatupd = 'f' where usesuper = 'f'\" template1" su postgres -c "psql -c \"update pg_shadow set usecatupd = 't' where usesuper = 't'\" template1" ) fi if [ "$1" = "configure" ] then if [ -d /usr/doc -a ! -e /usr/doc/postgresql -a -d /usr/share/doc/postgresql ] then ln -sf ../share/doc/postgresql /usr/doc/postgresql fi fi exit 0