-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-create-postgresql
95 lines (75 loc) · 2.45 KB
/
docker-create-postgresql
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
#!/bin/bash
LANG=C
cmdname=`basename $0`
newtmpdir=`mktemp -d /tmp/${cmdname}.XXXXXX`
user=`whoami`
OS=`uname`
[ "${user}" != "root" ] && echo "sudo ${cmdname}" && exit 1;
function cleanup () {
rm -rf "${newtmpdir}"
}
docker_bin=`which docker`;
if [ "$docker_bin" == "" ]
then
echo "Docker not installed. Proceed with install? (CTRL+C to Abort)"
echo "or run install manually: apt-get -y install docker-ce"
read
apt-get -y install docker-ce
fi
function usage() {
cat <<EOF
Usage: ${cmdname}
Interactive script to download and setup PostgreSQL docker configuration.
Will ask for PostgreSQL PORT and VERSION, and will create new service with automatic startup across reboots.
-h --help : Help (this screen).
EOF
}
trap 'cleanup' EXIT
trap 'cleanup' SIGTERM
case "$1" in
-h|--help)
usage
exit
;;
esac
echo "Please enter database PORT (eg 5432), CTRL+C to abort: "
read docker_port
[ "${docker_port}" == "" ] && echo "Error: Empty port!" && exit 1
if lsof -i:${docker_port} |grep -q LISTEN
then
echo "ERROR: Port ${docker_port} allready in use!"
lsof -i:${docker_port} |grep LISTEN
exit 1
fi
echo "Please enter database VERSION (eg 13), CTRL+C to abort: "
read docker_pgversion
[ "${docker_pgversion}" == "" ] && echo "Error: Empty VERSION!" && exit 1
docker_superuser=postgres
docker_servicename="postgresql-${docker_port}"
docker_pass=`tr -dc "a-zA-Z0-9" < /dev/urandom | head -c 10`
docker_path="/var/lib/${docker_servicename}"
mkdir -p ${docker_path}
[ "$OS" == "Darwin" ] && chmod 777 ${docker_path}
echo "created: ${docker_path}"
docker run --restart unless-stopped -d --name ${docker_servicename} -e POSTGRES_USER=${docker_superuser} -e POSTGRES_PASSWORD=${docker_pass} -e PGDATA=/var/lib/postgresql/data/pgdata -v ${docker_path}:/var/lib/postgresql/data -p ${docker_port}:5432 postgres:${docker_pgversion}
[ "$OS" == "Darwin" ] && chmod 600 ~/.pgpass
echo "localhost:${docker_port}:*:postgres:${docker_pass}" >> ~/.pgpass
chmod 400 ~/.pgpass
cat <<EOF
OK - new PostgreSQL server version ${docker_pgversion} created:
host = 127.0.0.1
port = ${docker_port}
user = postgres
password = ${docker_pass} (saved to ~/.pgpass)
data = ${docker_path}
This information is copied to /etc/info.
EOF
cat >> /etc/info <<EOF
PostgreSQL server version ${docker_pgversion} running:
host = 127.0.0.1
port = ${docker_port}
user = ${docker_superuser}
password = ${docker_pass} (saved to ~/.pgpass)
data = ${docker_path}
EOF
chmod 600 /etc/info