summaryrefslogtreecommitdiffstats
path: root/postgresql-check-db-dir.in
blob: f8adc2d5dd9d40145043c0bbb5adca04ca229504 (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
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
#!/bin/bash

# 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.

if test -z "$1"; then
    echo "Maintainer error: $0 must be run with one argument"
    exit 1
fi

service_name="$1"

if [ -z "$PGDATA" ]; then
    echo $"You try to start '$service_name' service"
    echo $"but the required \$PGDATA environment variable is not set."
    echo $"You should use the @userunitsdir@/$service_name.service.d/ANYTHING.conf"
    echo $"configuration file to set \$PGDATA.  For more info see"
    echo $"the @README_DIST@ file."

    exit 1
fi

# Warn the user that the configuration should be adjusted via drop-in, in case
# the $PGDATA variable is set different way (and non-default service name is
# used).
conf_dir="@userunitsdir@/$service_name.service.d"
if [[ "$service_name" == *@* ]] &&  test ! -d "$conf_dir"; then
    echo >&2 $"WARNING: Note that the '$conf_dir'"
    echo >&2 $"directory does not exist while you are using non-default service"
    echo >&2 $"name '$service_name'.  You are encouraged to use the"
    echo >&2 $"$conf_dir directory for systemd configuration according"
    echo >&2 $"to @README_DIST@ documentation."
fi

# Full PostgreSQL version, e.g. 9.0.2
PGVERSION=@PGVERSION@

# Major version of PostgreSQL, e.g. 9.0
PGMAJORVERSION=@PGMAJORVERSION@

# Previous major version, e.g., 8.4, for upgrades
PREVMAJORVERSION=@PREVMAJORVERSION@

# Distribution README file
README_DIST=@README_DIST@

# 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 $"Use \"postgresql-setup --upgrade\" to upgrade to version $PGMAJORVERSION."
        echo $"See $README_DIST for more information."
        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 $README_DIST for more information."
        exit 1
    fi
else
    # No existing PGDATA! Warn the user to initdb it.
    echo $"\"$PGDATA\" is missing or empty."
    echo $"Use \"postgresql-setup --initdb\" to initialize the database cluster."
    echo $"See $README_DIST for more information."
    exit 1
fi

exit 0