aboutsummaryrefslogtreecommitdiffstats
path: root/meta-openstack/recipes-support/postgresql/postgresql/postgresql-init
blob: cc7b13ef943c64867d0f736b69f76a495059e1ff (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
#!/bin/sh
# set -x

PN=postgresql
CONTROLLER_IP=%CONTROLLER_IP%
COMPUTE_IP=%COMPUTE_IP%
DB_USER=%DB_USER%
DB_PASSWORD=%DB_PASSWORD%
DATA_DIR=%DB_DATADIR%

if [ ! -e $DATA_DIR ]; then
	mkdir -p $DATA_DIR
	chown postgres $DATA_DIR
fi

if [ -e $DATA_DIR/PG_VERSION ]; then
    # the database has already been initialized, return
    exit 0
fi

# Create the DB
sudo -u postgres initdb -D $DATA_DIR

# Allow readers/writers by IP
echo "listen_addresses = '*'" >> $DATA_DIR/postgresql.conf
echo "host   all   all   ${CONTROLLER_IP}/32   trust" >> $DATA_DIR/pg_hba.conf
echo "host   all   all   ${COMPUTE_IP}/32   trust" >> $DATA_DIR/pg_hba.conf
systemctl restart postgresql

count=0
done=0
while [ $count -le 10 ] && [ $done -eq 0 ]; do
    sudo -u postgres psql -c "CREATE ROLE ${DB_USER} WITH SUPERUSER LOGIN PASSWORD '${DB_PASSWORD}'" 2> /dev/null
    if [ $? -ne 0 ]; then
        echo "[INFO] postgres: failed to create account for ${DB_USER}, trying again"
        systemctl stop postresql
	sleep 3
        systemctl start postgresql
	sleep 3
    else
        echo "[INFO] postgres: created account for ${DB_USER}, continuing .. "
        done=1
    fi
    count=`expr $count + 1`
done
    
if [ $done -eq 0 ]; then
    echo "[ERROR] postgres: unable to create admin account"
    exit 1
fi

ln -s /usr/share/zoneinfo /usr/share/postgresql/timezone || true