summaryrefslogtreecommitdiff
path: root/testing/postgresql/postgresql-initdb
blob: a691a83ff10e362ce6c89783ea7971ffe2349b2b (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
#!/bin/sh

set -e

. /etc/conf.d/postgresql

# 2nd clause is necessary to prevent symlinking the directory to itself when it
# doesn't exist yet
if [ ! /var/lib/postgres -ef "$PGROOT" ] && [ /var/lib/postgres != "$PGROOT" ]; then
    echo "Creating symlink /var/lib/postgres -> $PGROOT"

    # Remove /var/lib/postgres if empty dir, but not if symlink
    if [ ! -L /var/lib/postgres ] && [ -d /var/lib/postgres ]; then
        rmdir /var/lib/postgres
    fi

    ln -sf "$PGROOT" /var/lib/postgres
fi

PGDATA="$PGROOT/data"

if [ ! -d "$PGDATA" ]; then
    echo "Initializing database in $PGDATA"

    mkdir -p "$PGDATA"
    chown -R postgres:postgres "$PGDATA"

    su - postgres -m -c "/usr/bin/initdb $INITOPTS -D '$PGDATA'" >/dev/null

    if [ -f /etc/postgresql/postgresql.conf ]; then
        ln -sf /etc/postgresql/postgresql.conf "$PGDATA/postgresql.conf"
    fi
fi