This repository has been archived by the owner on Mar 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathubuntu-postgres.sh
53 lines (46 loc) · 1.76 KB
/
ubuntu-postgres.sh
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
#!/usr/bin/env bash
set -e
# {{{ PostgreSQL
postgres-remote-access-allow() {
log-operation "$FUNCNAME" "$@"
$SUDO sed -e "s/[#]\?listen_addresses = .*/listen_addresses = '*'/g" -i '/etc/postgresql/'*'/main/postgresql.conf'
$SUDO grep '192.168.1.1' '/etc/postgresql/'*'/main/pg_hba.conf' | \
( echo 'host all all 192.168.1.1/22 md5' | $SUDO tee -a '/etc/postgresql/'*'/main/pg_hba.conf' >/dev/null )
}
postgres-password-reset() {
log-operation "$FUNCNAME"
$SUDO -u 'postgres' psql -c "ALTER ROLE postgres WITH ENCRYPTED PASSWORD '$1'" 1>/dev/null
}
postgres-autovacuum-on() {
log-operation "$FUNCNAME"
$SUDO sed -e "s/[#]\?autovacuum = .*/autovacuum = on/g" -i '/etc/postgresql/'*'/main/postgresql.conf'
$SUDO sed -e "s/[#]\?track_counts = .*/track_counts = on/g" -i '/etc/postgresql/'*'/main/postgresql.conf'
}
postgres-template-encoding() {
local encoding
local ctype
local collate
encoding="${1:-UTF8}"
ctype="${2:-en_GB.$( echo "$encoding" | tr 'A-Z' 'a-z' )}"
collate="${3:-$ctype}"
log-operation "$FUNCNAME" "$@"
SQL_TMP_FILE="$( mktemp -t 'sql-XXXXXXXX' )"
cat > "$SQL_TMP_FILE" <<-EOD
UPDATE pg_database SET datallowconn = TRUE WHERE datname = 'template0';
\\c template0;
UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';
DROP DATABASE template1;
CREATE DATABASE template1 WITH template = template0 ENCODING = '${encoding}' LC_CTYPE = '${ctype}' LC_COLLATE = '${collate}';
UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';
\\c template1;
UPDATE pg_database SET datallowconn = FALSE WHERE datname = 'template0';
EOD
$SUDO chmod 0777 "$SQL_TMP_FILE"
$SUDO -u 'postgres' psql -f "$SQL_TMP_FILE" 1>/dev/null || {
EXIT_CODE=$?
rm "$SQL_TMP_FILE"
exit $EXIT_CODE
}
rm "$SQL_TMP_FILE"
}
# }}}