-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg-install
executable file
·97 lines (85 loc) · 2.44 KB
/
pg-install
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/sh
set -e
set -u
fn_help() { (
echo "pg-install v1.0.0 - installs postgres via apt-get or apk"
echo ""
echo "USAGE"
echo " pg-install <version>"
echo ""
echo "EXAMPLE"
echo " pg-install 16"
echo ""
echo "COPYING"
echo " Copyright (c) 2024 AJ ONeal <aj@bnna.net>"
echo " Licensed under the MPL-2.0"
); }
g_sudo=""
if command -v sudo > /dev/null; then
g_sudo="sudo"
fi
fn_pg_find_or_install() { (
if command -v psql; then
return 0
fi
a_pgver="${1}"
if command -v apt-get > /dev/null; then
${g_sudo} apt-get install -y postgresql-"${a_pgver}" postgresql-client-"${a_pgver}"
elif command -v apk; then
${g_sudo} apk add --no-cache postgresql"${a_pgver}" postgresql"${a_pgver}"-client
else
echo >&2 "error: please install psql / postgres manually"
return 1
fi
); }
fn_pg_set_default_user() { (
if test -n "${g_sudo}"; then
${g_sudo} -u postgres sh -c 'echo "ALTER USER \"postgres\" WITH PASSWORD '\'postgres\''" | psql postgres'
else
echo "ALTER USER \"postgres\" WITH PASSWORD 'postgres'" | psql postgres
fi
); }
fn_init_pgpass() { (
if ! test -e ~/.pgpass; then
touch ~/.pgpass
fi
if ! grep -q -i '^\s*#.*port:d' ~/.pgpass; then
echo '# hostname:port:database:username:password' >> ~/.pgpass.header.txt
mv ~/.pgpass ~/.pgpass.noheader.bak
cat ~/.pgpass.header.txt ~/.pgpass.noheader.bak > ~/.pgpass
rm ~/.pgpass.header.txt ~/.pgpass.noheader.bak
fi
if ! grep -q '^\s*#\s*export\s\+PGPASSFILE=' ~/.pgpass; then
echo "# export PGPASSFILE='${HOME}/.pgpass'" >> ~/.pgpass.header.txt
mv ~/.pgpass ~/.pgpass.noheader.bak
cat ~/.pgpass.header.txt ~/.pgpass.noheader.bak > ~/.pgpass
rm ~/.pgpass.header.txt ~/.pgpass.noheader.bak
fi
chmod 0600 ~/.pgpass
); }
main() { (
case ${1:-} in
--help | help)
fn_help
return 0
;;
-V | --version | version)
fn_help
return 0
;;
"")
fn_help >&2
return 1
;;
*) ;;
esac
g_pgver="${1:-}"
fn_pg_find_or_install "${g_pgver}" >&2
# command -v psql
# /usr/lib/postgresql/16/bin/psql
fn_pg_set_default_user >&2
#echo "ALTER USER \"postgres\" WITH PASSWORD 'postgres'"
fn_init_pgpass >&2
#echo '~'/.pgpass
); }
main "${@:-}"