summaryrefslogtreecommitdiff
path: root/extra/postgresql/postgresql-check-db-dir
blob: 542c82209bc8fb54cd1e9030ecc2ea12e40f731e (plain)
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
#!/bin/sh

# This script verifies that the postgresql data directory has been correctly
# initialized.  We do not want to automatically initdb it, because that has
# a risk of catastrophic failure (ie, overwriting a valuable database) in
# corner cases, such as a remotely mounted database on a volume that's a
# bit slow to mount.  But we can at least emit a message advising newbies
# what to do.

PGDATA="$1"

if [ -z "$PGDATA" ]
then
    echo "Usage: $0 database-path"
    exit 1
fi

# PGMAJORVERSION is major version
PGMAJORVERSION=9.2
# PREVMAJORVERSION is the previous major version, e.g., 8.4, for upgrades
PREVMAJORVERSION=9.1

# Check for the PGDATA structure
if [ -f "$PGDATA/PG_VERSION" ] && [ -d "$PGDATA/base" ]
then
    # Check version of existing PGDATA
    if [ x`cat "$PGDATA/PG_VERSION"` = x"$PGMAJORVERSION" ]
    then
        : A-OK
    elif [ x`cat "$PGDATA/PG_VERSION"` = x"$PREVMAJORVERSION" ]
    then
        echo $"An old version of the database format was found."
        echo $"See https://wiki.archlinux.org/index.php/PostgreSQL#Upgrading_PostgreSQL"
        exit 1
    else
        echo $"An old version of the database format was found."
        echo $"You need to dump and reload before using PostgreSQL $PGMAJORVERSION."
        echo $"See http://www.postgresql.org/docs/9.2/static/upgrading.html"
        exit 1
    fi
else
    # No existing PGDATA! Warn the user to initdb it.
    echo $"\"$PGDATA\" is missing or empty. Use a command like"
    echo $"  su - postgres -c \"initdb --locale en_US.UTF-8 -D '$PGDATA'\""
    echo $"with relevant options, to initialize the database cluster."
    exit 1
fi

exit 0