File: postinst.in

package info (click to toggle)
postgresql 7.4.7-6sarge6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 11,168 kB
  • ctags: 27
  • sloc: sh: 1,903; makefile: 337; ansic: 204; perl: 69; sed: 6; tcl: 1
file content (599 lines) | stat: -rw-r--r-- 16,294 bytes parent folder | download
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#!/bin/bash
set -e
set +o noclobber

# Load the debconf library - this must come FIRST; do not insert any other
# commands before this
. /usr/share/debconf/confmodule

#include <genscript.warning>
#include <dumpall_loc.inc>
# postgresql Debian package - post-installation script
#

check_pg_hba_conf() {
    # Look for lines in pg_hba.conf without the user column; this indicates the
    # file is out of date
    if grep -qsE '^[ 	]*local[ 	]+[^ 	]*[ 	]+(trust|ident|reject|md5|password|crypt|krb5|pam)' /etc/postgresql/pg_hba.conf
    then
	return 1
    fi
    if grep -qsE '^[ 	]*(host|hostssl)[ 	]+[^ 	]+[ 	]+[^ 	]+[ 	]+[^/ 	]+[ 	]+(trust|ident|reject|md5|password|crypt|krb5|pam)' /etc/postgresql/pg_hba.conf
    then
	return 1
    fi
    return 0
}

# remove conffiles in PGDATA and symlink them to /etc/postgresql, and adjust
# permissions
setup_conffiles() {
    # Make a symbolic link to the configuration files
    for f in pg_hba.conf pg_ident.conf postgresql.conf
    do
        if [ ! -L ${PGDATA}/$f ]
        then
            rm -f ${PGDATA}/$f
            ln -s /etc/postgresql/$f ${PGDATA}/$f
        fi
    done

    # set configuration file permissions
    chown root.postgres /etc/postgresql/{pg_hba.conf,pg_ident.conf} || true
    chmod 640 /etc/postgresql/{pg_hba.conf,pg_ident.conf} || true
}

# attempt automatic upgrade from version $1
do_upgrade() {
    echo "
A database upgrade appeared to be required and an automatic upgrade was
attempted.
" >> $MAILFILE
    echo "
Attempting PostgreSQL database upgrade; the time taken will depend on the
size of your database(s)." >&2

    # Automatic upgrade locations
    db_get postgresql/upgrade/dump_location
    DUMPLOC=${RET:-$PGDATA/..}
    db_get postgresql/upgrade/preserve_location
    PRESERVELOC=${RET:-$PGDATA/../preserve}
    # create it if it doesn't exist
    if [ ! -d "$DUMPLOC" ]
    then
	install -d "$DUMPLOC"
	chown postgres.postgres "$DUMPLOC"
    fi
    if ! [ -d "$PRESERVELOC" ]
    then
	install -d "$PRESERVELOC"
	chown postgres.postgres "$PRESERVELOC"
    fi

    # Make sure we don't overwrite an earlier preserved database
    i=
    ok=false
    while [ $ok = false ]
    do
	export savedir=${PRESERVELOC}/data.$1.save$i
	if [ ! -e $savedir ]
	then
	    ok=true
	else
	    [ -z "$i" ] && i=1 || i=`expr $i + 1`
	fi
    done

    # Try the automatic upgrade
    cat <<EOI > $SCRIPTFILE
#!/bin/sh
unset PGHOST
export PATH=/usr/lib/postgresql/bin:/bin:/usr/bin
postgresql-dump -inldvp $savedir -Ft "${DUMPLOC}/db.out" > $TMPFILE 2>&1
EOI

    if /sbin/start-stop-daemon --chuid postgres --name `basename $SCRIPTFILE` --startas /bin/sh --start -- $SCRIPTFILE
    then
        setup_conffiles

	if [ -x /usr/sbin/invoke-rc.d ]
	then
	    /usr/sbin/invoke-rc.d postgresql restart || true
	else
	    /etc/init.d/postgresql restart || true
	fi
	sleep 5
	if pidof /usr/lib/postgresql/bin/postmaster > /dev/null
	then
	    # Success!
	    rm /var/lib/postgres/dumpall/default_encoding
	fi
    fi

    if ! pidof /usr/lib/postgresql/bin/postmaster > /dev/null
    then
	echo "Sorry; the automatic database upgrade failed." >&2
	echo "
The automatic upgrade did not succeed; the postmaster has not been
restarted.  The commands run (by the user postgres) were:

   unset PGHOST
   export PATH=/usr/lib/postgresql/bin:/bin:/usr/bin
   postgresql-dump -ildvp $savedir -Ft ${DUMPLOC}/db.out

The database requires manual intervention before it can be used.

The dump of the old data should be at:

   ${DUMPLOC}/db.out

The previous database should have been preserved at:

   ${savedir}
  
This was the output of the dump command:

======================================================================
" >>$MAILFILE
	cat $TMPFILE >> $MAILFILE
	MAILSUBJECT="postgresql automatic upgrade failed"
    else
	echo "
The automatic upgrade of the PostgreSQL database appears
to have succeeded.
" > $MAILFILE
	MAILSUBJECT="postgresql automatic upgrade succeeded"
    fi
}


# return matching database encoding for PGLANG
get_encoding () {
    CHARSET=`env LANG=$PGLANG locale charmap`
    MAP=$(grep -i "\<$CHARSET\>" <<EOF | head -1 | cut -d' '  -f 1
EUC_JP EUC-JP
EUC_JP eucJP
EUC_JP IBM-eucJP
EUC_JP sdeckanji
EUC_CN EUC-CN
EUC_CN eucCN
EUC_CN IBM-eucCN
EUC_CN GB2312
EUC_CN dechanzi
EUC_KR EUC-KR
EUC_KR eucKR
EUC_KR IBM-eucKR
EUC_KR deckorean
EUC_KR 5601
EUC_TW EUC-TW
EUC_TW eucTW
EUC_TW IBM-eucTW
EUC_TW cns11643
UTF8 UTF-8
UTF8 utf8
LATIN1 ISO-8859-1
LATIN1 ISO8859-1
LATIN1 iso88591
LATIN2 ISO-8859-2
LATIN2 ISO8859-2
LATIN2 iso88592
LATIN3 ISO-8859-3
LATIN3 ISO8859-3
LATIN3 iso88593
LATIN4 ISO-8859-4
LATIN4 ISO8859-4
LATIN4 iso88594
LATIN5 ISO-8859-9
LATIN5 ISO8859-9
LATIN5 iso88599
LATIN6 ISO-8859-10
LATIN6 ISO8859-10
LATIN6 iso885910
LATIN7 ISO-8859-13
LATIN7 ISO8859-13
LATIN7 iso885913
LATIN8 ISO-8859-14
LATIN8 ISO8859-14
LATIN8 iso885914
LATIN9 ISO-8859-15
LATIN9 ISO8859-15
LATIN9 iso885915
LATIN10 ISO-8859-16
LATIN10 ISO8859-16
LATIN10 iso885916
ISO_8859_5 ISO-8859-5
ISO_8859_5 ISO8859-5
ISO_8859_5 iso88595
ISO_8859_6 ISO-8859-6
ISO_8859_6 ISO8859-6
ISO_8859_6 iso88596
ISO_8859_7 ISO-8859-7
ISO_8859_7 ISO8859-7
ISO_8859_7 iso88597
ISO_8859_8 ISO-8859-8
ISO_8859_8 ISO8859-8
ISO_8859_8 iso88598
WIN CP1251
WIN1256 CP1256
TCVN CP1258
KOI8 KOI8-R
ALT CP866
EOF)
    if [ -n "$MAP" ]; then
	export ENCODING=$MAP
    else
	# fallback if no mapping is found
	export ENCODING=SQL_ASCII
    fi
}

db_security_update_CAN_2005_1409_1410() {
    for db in $(su -c '/usr/bin/psql -lt | cut -d\| -f1' postgres); do
        if [ "$db" = template0 ]; then
            continue
        fi

        echo -n "Applying security update in database $db..."
        if su -c "/usr/bin/psql $db" postgres >/dev/null 2>/dev/null <<EOF
        UPDATE pg_proc
          SET proacl = '{=}'
          WHERE pronamespace = 11 and pronargs = 5 and proargtypes[2] = 'cstring'::regtype;

        UPDATE pg_proc 
          SET proargtypes[0] = 'internal'::regtype
          WHERE oid in (
            'dex_init(text)'::regprocedure,
            'snb_en_init(text)'::regprocedure,
            'snb_ru_init(text)'::regprocedure,
            'spell_init(text)'::regprocedure,
            'syn_init(text)'::regprocedure
          );
EOF
        then
            echo "success"
        else
            echo "FAILED! Please fix manually (contact postgresql@packages.debian.org if necessary)"
        fi
    done

    echo "Applying security update in database template0..."
    su -c "/usr/bin/psql template1" postgres >/dev/null 2>/dev/null <<EOF 
    UPDATE pg_database SET datallowconn = true WHERE datname = 'template0';
    \connect template0;
    UPDATE pg_proc SET proacl = '{=}'
    WHERE pronamespace = 11 AND pronargs = 5
        AND proargtypes[2] = 'cstring'::regtype;
    VACUUM FREEZE;
    \connect template1;
    UPDATE pg_database SET datallowconn = false WHERE datname = 'template0';
EOF
}

# *** EXECUTION STARTS HERE ***
SHELL=/bin/sh
case "$1" in
    configure | reconfigure)
	;;
    *)
	# in the future this needs to be improved!
	echo Nothing to do for $1
	exit
esac

SCRIPTFILE=`mktemp -t pg.XXXXXX` || exit 1
chown postgres:postgres $SCRIPTFILE
chmod 700 $SCRIPTFILE
TMPFILE=`mktemp -t pg.XXXXXX` || exit 1
chown postgres:postgres $TMPFILE
MAILFILE=`mktemp -t pg.XXXXXX` || exit 1
MAILSUBJECT="Postgresql installation"

# arrange to delete the temporary files and mail output to root
# when this script terminates
trap 'rm -f ${TMPFILE} ${SCRIPTFILE}; ([ -s $MAILFILE ] && mail -s "$MAILSUBJECT" root < ${MAILFILE} || true); rm -f $MAILFILE' 0

chmod a+x /etc/init.d/postgresql

# Check pg_hba.conf for the new format (from 7.3)
if ! check_pg_hba_conf
then
    db_get postgresql/convert-pg_hba.conf
    if [ "$RET" = true ]
    then
	/usr/lib/postgresql/bin/convert.pg_hba.conf >$TMPFILE
	cp /etc/postgresql/pg_hba.conf /etc/postgresql/pg_hba.conf.pre-7.3
	install -o root -g root -m 0644 $TMPFILE /etc/postgresql/pg_hba.conf
    else
	echo "
The format of the configuration file /etc/postgresql/pg_hba.conf has 
changed, and you have not allowed the installation to update it.

This configuration must therefore be updated manually, before you
can start postgresql.  Look at /etc/postgresql/pg_hba.conf.dpkg-dist for the
new format.
"  >> $MAILFILE

	MAILSUBJECT="Postgresql - obsolete configuration"
    fi
fi

# get PGDATA

if [ -f /etc/postgresql/postmaster.conf ]
then
    # get PGDATA from already existing file
    . /etc/postgresql/postmaster.conf
    PGDATA="${POSTGRES_DATA:=/var/lib/postgres/data}"
else
    # new installation, get PGDATA from debconf
    db_get postgresql/initdb/location || true
    PGDATA="${RET:-/var/lib/postgres/data}"

    # purge old configuration file from ucf just to be sure
    ucf --purge /etc/postgresql/postmaster.conf
fi

# Install postmaster.conf using the template and ucf

PMCONF="/etc/postgresql/postmaster.conf.dpkg-new"
sed "s|@POSTGRES_DATA@|$PGDATA|" < /usr/share/postgresql/postmaster.conf.in > $PMCONF

ucf --debconf-ok --three-way $PMCONF /etc/postgresql/postmaster.conf
rm -f $PMCONF

# read postmaster.conf again to get new settings and POSTGRES_HOME
. /etc/postgresql/postmaster.conf
PGDATA="${POSTGRES_DATA:=/var/lib/postgres/data}"
PGHOME="${POSTGRES_HOME:=/var/lib/postgres}"

PGBASE=/usr/lib/postgresql
PGLIB=${PGBASE}/lib
PATH=${PATH}:${PGBASE}/bin

export PGDATA PGLIB PATH

if [ "$PGHOME" = '/' ]
then
    echo "Error: POSTGRES_HOME must not be the root directory '/'!
If you did not change the default setting in /etc/postgresql/postmaster.conf,
POSTGRES_HOME is assumed to be the home directory of the 'postgres' user,
which probably is '/'. Please correct this and install again.
" >&2
    exit 1
fi

if [ ! -d "${PGHOME}" -a ! -f "${PGHOME}" ]
then
    echo Creating missing home directory ${PGHOME} for postgres >&2
    install -m 700 -o postgres -g postgres -d ${PGHOME}
fi

if [ ! -d "${PGHOME}" ]
then
    echo Cannot create home directory ${PGHOME} for postgres >&2
    exit 1
fi

# Make sure that postgres' home directory exists
PSWDHOME=`getent passwd postgres | cut -f6 -d:`
if [ ! -e "${PSWDHOME}" ]
then
    echo "The home directory of user 'postgres' (${PSWDHOME}) does not exist.

(The home directory from /etc/postgresql/postmaster.conf is ${PGHOME})
" >> $MAILFILE
    MAILSUBJECT="postgres home directory missing"
    exit 1
fi

PGSHELL=`getent passwd postgres | 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.conf ${PGHOME}/${PROFILE} ||
    (echo . /etc/postgresql/postmaster.conf >>${PGHOME}/${PROFILE}
    if [ ${PROFILE} = .login ]
    then
	# C-shell syntax...
	echo setenv PATH /bin:/usr/bin:${PGBASE}/bin >>${PGHOME}/${PROFILE}
	echo setenv POSTGRES_DATA ${POSTGRES_DATA} >>${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 POSTGRES_DATA=${POSTGRES_DATA} >>${PGHOME}/${PROFILE}
	echo PGDATA=\${POSTGRES_DATA:-/var/lib/postgres/data} >>${PGHOME}/${PROFILE}
	echo PGLIB=${PGLIB} >>${PGHOME}/${PROFILE}
	echo export PGLIB PGDATA >>${PGHOME}/${PROFILE}
    fi)

# adjust permissions of data directories
chown -R postgres.postgres ${PGDATA} ${PGHOME} /var/run/postgresql || true
chmod 775 /var/run/postgresql
chmod 700 ${PGDATA}

if [ ! -f ${PGDATA}/PG_VERSION ]
then
    # There is no existing database structure
    db_get postgresql/settings/locale
    PGLANG=${RET:-${LANG:-C}}

    # Determine matching encoding
    get_encoding

    # Install the PostgreSQL database files in ${PGDATA}
    cat <<EOI > $SCRIPTFILE
#!/bin/sh
cd ${PGHOME}
. ./${PROFILE}
unset LC_ALL LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT LC_IDENTIFICATION
export LANG=$PGLANG

initdb --encoding ${ENCODING} --pgdata ${PGDATA}
EOI

    /sbin/start-stop-daemon --chuid postgres --name `basename $SCRIPTFILE` --start --startas /bin/sh -- $SCRIPTFILE
    
    setup_conffiles

    echo "
A new PostgreSQL database structure was installed.

Use /usr/bin/createdb to create a specific database and
/usr/bin/createuser to enable other users to connect to a
PostgreSQL database. 

In the first instance, these commands must be run by the
user 'postgres'.
" >> $MAILFILE
MAILSUBJECT="PostgreSQL installation"
fi

# Install postgresql.conf using the template and ucf

# if we are not upgrading, purge postgresql.conf from ucf's cache
[ -f /etc/postgresql/postgresql.conf ] || ucf --purge /etc/postgresql/postgresql.conf

db_get postgresql/settings/day_month_order
DateOrder=${RET:-European}

PGCONF="/etc/postgresql/postgresql.conf.dpkg-new"
sed "s/@DAYMONTHORDER@/$DateOrder/" < /usr/share/postgresql/postgresql.conf.in > $PGCONF

ucf --debconf-ok --three-way $PGCONF /etc/postgresql/postgresql.conf
rm -f $PGCONF

# 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
if [ -x /usr/sbin/invoke-rc.d ]
then
    RESTARTOUT=$(/usr/sbin/invoke-rc.d --disclose-deny postgresql restart 2>&1)
    invokestatus=$?
else
    RESTARTOUT=$(/etc/init.d/postgresql restart 2>&1)
    invokestatus=$?
fi
if [ $invokestatus -ne 0 ]
then
    if [ $invokestatus -eq 101 ]
    then
            echo PostgreSQL disabled by administrator.  Use update-rc.d or file-rc to enable it.
            exit 0
    else
            echo "$RESTARTOUT" >&2
            exit $invokestatus
    fi
fi
set -e


# take a little nap while the postmaster sorts itself out
sleep 5

if ! pidof /usr/lib/postgresql/bin/postmaster >/dev/null
then
    # postmaster isn't running, so we probably have an old database to update
    if ! echo "$RESTARTOUT" | grep -q 'is in an older format' 
    then
	if [ "$invokestatus" ] && [ $invokestatus -eq 0 ]
	then
	    # there are several reports that invoke-rc.d is a link to true,
	    # exit cleanly in this case.
	    echo "postgresql failed to start. /usr/sbin/invoke-rc.d is probably broken." >&2
	    echo "$RESTARTOUT" >&2
	    exit 0
	else
	    echo "postgresql failed to start because of an unknown problem:" >&2
	    echo "$RESTARTOUT" >&2
	    exit 1
	fi
    fi

    # lets see if we can do it automatically...
    if [ -f ${PGDATA}/PG_VERSION ] 
    then
	# It looks possible
	echo "
The postmaster did not start after postgresql was installed:
" >> $MAILFILE
        echo "$RESTARTOUT" >>$MAILFILE
	MAILSUBJECT="PostgreSQL installation - database upgrade"

	db_get postgresql/upgrade/policy || true
	if [ "$RET" = true ]
	then
	    do_upgrade $2
	else
	    echo "
The newly-installed version of PostgreSQL requires a dump and reload
of the databases.  You have chosen not to allow this to be done
automatically.

Your data has not been updated and the postmaster is not running.

Read /usr/share/doc/postgresql/README.Debian.migration.gz and do a
manual update.  Then restart the postmaster with
\'invoke-rc.d postgresql restart'." >>$MAILFILE
	    MAILSUBJECT="postgresql databases need upgrading"
	fi
    else
	echo "
Unknown problem.

postgresql has failed to create a new database, nor is there any old
database present.  (Searched for ${PGDATA}/PG_VERSION)

The message when attempting to start postgresql was:
" >&2
	cat $TMPFILE >&2
	exit 1
    fi
fi

# Only do the next bit if the postmaster is running...
if pidof /usr/lib/postgresql/bin/postmaster > /dev/null
then
	# ...and postgresql-client has been installed
	if [ -x /usr/bin/psql -a -x /usr/lib/postgresql/bin/psql ]
	then
		db_get medium postgresql/enable_lang || true
		if [ "$RET" = true ]
		then
		# Enable the PLPGSQL language in all PostgreSQL databases...
			/sbin/start-stop-daemon --chuid postgres --name enable_lang --startas /usr/lib/postgresql/bin/enable_lang --start -- plpgsql --all >/dev/null 2>&1 || true
		fi
		# ...but never mind if it fails
	fi
fi

db_stop

# Apply security updates to existing databases
if [ "$2" ] && dpkg --compare-versions "$2" lt "7.4.7-6"; then
    db_security_update_CAN_2005_1409_1410
fi

exit 0