-
Notifications
You must be signed in to change notification settings - Fork 27
/
softwarecollections-db-setup
executable file
·54 lines (44 loc) · 1.51 KB
/
softwarecollections-db-setup
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
#!/bin/bash
NAME=softwarecollections
DATADIR=/var/lib/pgsql/$NAME
SOCKETDIR=/var/scls/db
PORT=5432
SOCKET=$SOCKETDIR/.s.PGSQL.$PORT
UNIT="postgresql@${NAME}"
SERVICE="${UNIT}.service"
log() {
if [ -t 1 ]; then
echo -e "\033[1;36m$1\033[0m"
else
echo "$1"
fi
}
if ! test -d /etc/systemd/system/$SERVICE.d; then
log "Create systemd unit"
postgresql-new-systemd-unit --unit "$UNIT" --datadir "$DATADIR"
log "Create datadir"
postgresql-setup --initdb --unit "$UNIT" --port $PORT
log "Configure database"
sed -r -i \
-e "s|^#* *listen_addresses.*|listen_addresses = ''|" \
-e "s|^#* *unix_socket_directories.*|unix_socket_directories = '$SOCKETDIR'|" \
$DATADIR/postgresql.conf
sed -r -i 's/(local +all +all +).*/\1trust/' $DATADIR/pg_hba.conf
fi
if ! systemctl is-enabled -q $SERVICE; then
log "Enable $SERVICE"
systemctl enable $SERVICE
fi
if ! systemctl is-active -q $SERVICE; then
log "Start $SERVICE"
systemctl start $SERVICE || \
systemctl status $SERVICE
fi
if test -S $SOCKET && ! echo | su - softwarecollections -c "psql -h $SOCKETDIR -p $PORT -U softwarecollections" 2>/dev/null; then
log "Create database user"
su - postgres -c "createuser -h $SOCKETDIR -p $PORT softwarecollections"
fi
if test -S $SOCKET && ! echo | su - softwarecollections -c "psql -h $SOCKETDIR -p $PORT -d softwarecollections" 2>/dev/null; then
log "Create database"
su - postgres -c "createdb -h $SOCKETDIR -p $PORT softwarecollections"
fi