File: postgresql.postinst

package info (click to toggle)
postgresql 7.5.22
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 56 kB
  • sloc: sh: 168; makefile: 21
file content (178 lines) | stat: -rw-r--r-- 6,128 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
#!/bin/sh -e

#DEBHELPER#

[ "$1" = configure ] || exit 0

remove_unchanged_conffile() {
    [ -e "$1" ] || return 0
    old_md5=`grep "^ $1 " /var/lib/dpkg/status | sed -e "s/.* //"`
    new_md5=`md5sum "$1" | sed -e "s/ .*//"`
    if [ "$old_md5" = "$new_md5" ]; then
        rm "$1"
    else
	mv "$1" "$1.dpkg-old"
    fi
}

# everything that needs to be done only for the Sarge -> Etch transition
sarge_etch_upgrade() {
    # remove fake postgresql.conf from preinst again
    FAKECONF=/etc/postgresql/7.4/main/postgresql.conf
    if [ -f $FAKECONF -a ! -s $FAKECONF ]; then
        rm -f $FAKECONF
    fi

    # determine data directory
    if [ -e /etc/postgresql/postmaster.conf ]; then
        . /etc/postgresql/postmaster.conf
    fi
    PGDATA=${POSTGRES_DATA:-"/var/lib/postgres/data"}
    PGLOG=${POSTGRES_LOG:-"/var/log/postgresql/postgres.log"}

    if [ ! -f "$PGDATA/PG_VERSION" ]; then
        echo "Not migrating invalid PostgreSQL data directory $PGDATA (no PG_VERSION)."
        exit 0
    fi

    # determine cluster name
    for i in main transition legacy; do
        if [ ! -d /etc/postgresql/7.4/$i ] || [ -z "$(ls /etc/postgresql/7.4/$i)" ]; then
            if [ ! -d /var/lib/postgresql/7.4/$i ]; then
                CLUSTER=$i
                break
            fi
        fi
    done

    if [ -z $CLUSTER ]; then
        echo "Error: all possible cluster names already exist" >&2
        exit 1
    fi

    # try to move data directory to canonical location (possible if on the same
    # partition); if not, just leave the data directory as it is

    mkdir -p /var/lib/postgresql/7.4/$CLUSTER
    chown postgres:postgres /var/lib/postgresql
    chmod 755 /var/lib/postgresql /var/lib/postgresql/7.4
    if ln "$PGDATA/PG_VERSION" /var/lib/postgresql/7.4/$CLUSTER 2>/dev/null; then
        rm /var/lib/postgresql/7.4/$CLUSTER/PG_VERSION
        rmdir /var/lib/postgresql/7.4/$CLUSTER
        mv "$PGDATA" /var/lib/postgresql/7.4/$CLUSTER
        PGDATA=/var/lib/postgresql/7.4/$CLUSTER
    else
        rmdir /var/lib/postgresql/7.4/$CLUSTER 2>/dev/null || true
        rmdir /var/lib/postgresql/7.4 2>/dev/null || true
        rmdir /var/lib/postgresql 2>/dev/null || true
    fi

    # integrate cluster
    /usr/bin/pg_createcluster -u postgres -g postgres -d "$PGDATA" 7.4 $CLUSTER

    # try move the log file
    if [ -e "$PGLOG" ]; then
        if cp "$PGLOG" /var/log/postgresql/postgresql-7.4-$CLUSTER.log; then
            rm -f "$PGLOG"
        fi
    fi

    # adapt dynamic_library_path

    CONF="/etc/postgresql/7.4/$CLUSTER/postgresql.conf"
    if [ -e "$CONF" ]; then
        sed -i '/^[[:space:]]*dynamic_library_path[[:space:]]*=/ s/\/usr\/lib\/postgresql\/lib/\/usr\/lib\/postgresql\/7.4\/lib/' $CONF
    fi

    # migrate custom settings in postmaster.conf
    ENV="/etc/postgresql/7.4/$CLUSTER/environment"
    if [ -e /etc/postgresql/postmaster.conf ]; then 
        echo "Adding custom settings from /etc/postgresql/postmaster.conf to $ENV..."
        env -i bash --noprofile --norc <<EOF
set -a
. /etc/postgresql/postmaster.conf
set +a
env | egrep -v '^(PWD|SHLVL|_|POSTGRES_DATA|POSTGRES_HOME|POSTMASTER_OPTIONS|AUTOVACUUM)' | sed "s/=/='/; s/$/'/" >> $ENV || true
EOF
    fi  

    # migrate custom settings in postgresql.env
    if [ -e /etc/postgresql/postgresql.env ]; then 
        echo "Adding custom settings from /etc/postgresql/postgresql.env to $ENV..."
        env -i bash --noprofile --norc <<EOF
. /etc/postgresql/postgresql.env
env | egrep -v '^(PWD|SHLVL|_|PGLIB|PGHOST|PGDATA|PGACCESS_HOME)' | sed "s/=/='/; s/$/'/" >> $ENV || true
EOF
    fi  

    # go!
    /usr/bin/pg_ctlcluster 7.4 $CLUSTER start

    echo "Your existing PostgreSQL cluster has been successfully integrated
into the new database infrastructure. All configuration is in
/etc/postgresql/7.4/$CLUSTER now. Please consult the postgresql-common(1) and
pg_lsclusters(1) manpages."
}

# cleanup actions that can be performed also in 7.5.x -> 7.5.x+1 upgrades
cleanup() {
    # remove obsolete stuff
    rm -rf /var/lib/postgres/dumpall

    remove_unchanged_conffile /etc/postgresql/postgresql.env
    remove_unchanged_conffile /etc/postgresql/postmaster.conf
    remove_unchanged_conffile /etc/init.d/postgresql
    remove_unchanged_conffile /etc/logrotate.d/postgresql
    remove_unchanged_conffile /etc/cron.d/postgresql
    remove_unchanged_conffile /etc/logcheck/ignore.d.workstation/postgresql
    remove_unchanged_conffile /etc/logcheck/ignore.d.paranoid/postgresql
    remove_unchanged_conffile /etc/logcheck/ignore.d.server/postgresql
    remove_unchanged_conffile /etc/logcheck/violations.ignore.d/logcheck-postgresql

    if [ -f /etc/init.d/postgresql.dpkg-old ]; then
        chmod a-x /etc/init.d/postgresql.dpkg-old
    fi

    PGHOME=$(getent passwd postgres|cut -f 6 -d:)
    for PROFILE in /var/lib/postgres/.profile /var/lib/postgres/.bash_profile "$PGHOME/.profile" "$PGHOME/.bash_profile"; do
        if [ -e "$PROFILE" ]; then
            # Remove stuff that is obsolete
            sed -i '/^. \/etc\/postgresql\/postmaster.conf/d; /^POSTGRES_DATA\s*=/d;
            /^PGDATA\s*=/d; /^PGLIB\s*=/d; /^export PGLIB PGDATA\s*$/d;
            /^PATH=\/bin:\/usr\/bin:\/usr\/lib\/postgresql\/bin\s*$/d' "$PROFILE"

            # Delete the file if only whitespace is left
            [ "$(sed '/^\s*$/d' "$PROFILE")" ] || rm -f "$PROFILE"
        fi
    done

    # add plpgsql.so symlink from the old lib directory
    if [ ! -e /usr/lib/postgresql/lib/plpgsql.so ]; then
	mkdir -p /usr/lib/postgresql/lib
	ln -s ../7.4/lib/plpgsql.so /usr/lib/postgresql/lib/plpgsql.so
    fi
}

###############
# entry point #
###############

# are we installing from scratch?
[ "$2" ] || exit 0

# upgrading from a too old version?
if dpkg --compare-versions "$2" lt 7.4; then
    echo "You try to upgrade from a version prior than 7.4, which is not
supported. Please remove this package and upgrade to Debian Sarge first." >&2
    exit 1
fi

# upgrading from Sarge?
if dpkg --compare-versions "$2" lt 7.5; then
    sarge_etch_upgrade
fi

# cleanup can always happen
cleanup

exit 0