-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgresql-gen.sh
104 lines (92 loc) · 3.88 KB
/
postgresql-gen.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
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
98
99
100
101
102
103
104
#!/bin/bash
# Define default values for optional settings
max_connections=100
shared_buffers=128MB
work_mem=4MB
maintenance_work_mem=64MB
enable_ssl="n"
include_postgis="n"
include_pgcrypto="n"
include_timescaledb="n"
custom_settings=""
# Define usage function
usage() {
echo "Usage: $0 -v <PostgreSQL version> [-p] [-c] [-t] [-s] [-n <max_connections>] [-b <shared_buffers>] [-w <work_mem>] [-m <maintenance_work_mem>] [-S] [-C <custom_settings>]"
echo "Options:"
echo " -v <version> PostgreSQL version (e.g., 13.4)"
echo " -p Include PostGIS extension (y/n)"
echo " -c Include pgcrypto extension (y/n)"
echo " -t Include TimescaleDB extension (y/n)"
echo " -s Enable SSL (y/n)"
echo " -n <max_connections> Maximum connections (default: 100)"
echo " -b <shared_buffers> Shared buffers size (default: 128MB)"
echo " -w <work_mem> Work mem size (default: 4MB)"
echo " -m <maintenance_work_mem> Maintenance work mem size (default: 64MB)"
echo " -S Include custom settings (y/n)"
echo " -C <custom_settings> Custom settings as a string (e.g., '-c custom_setting=value')"
exit 1
}
# Parse command line arguments
while getopts "v:p:c:t:s:n:b:w:m:SC:" opt; do
case $opt in
v) postgres_version="$OPTARG";;
p) include_postgis="y";;
c) include_pgcrypto="y";;
t) include_timescaledb="y";;
s) enable_ssl="y";;
n) max_connections="$OPTARG";;
b) shared_buffers="$OPTARG";;
w) work_mem="$OPTARG";;
m) maintenance_work_mem="$OPTARG";;
S) include_custom_settings="y";;
C) custom_settings="$OPTARG";;
\?) echo "Invalid option: -$OPTARG" >&2; usage;;
esac
done
# Check for required arguments
if [ -z "$postgres_version" ]; then
echo "Error: PostgreSQL version is required."
usage
fi
# Start generating Docker Compose file
echo "version: '3.8'" > docker-compose.yml
echo "services:" >> docker-compose.yml
echo " postgres:" >> docker-compose.yml
echo " image: postgres:${postgres_version}" >> docker-compose.yml
echo " environment:" >> docker-compose.yml
echo " POSTGRES_DB: mydb" >> docker-compose.yml
echo " POSTGRES_USER: myuser" >> docker-compose.yml
echo " POSTGRES_PASSWORD: mypassword" >> docker-compose.yml
# Add extensions based on user input
if [ "$include_postgis" == "y" ]; then
echo " volumes:" >> docker-compose.yml
echo " - ./initdb-postgis:/docker-entrypoint-initdb.d" >> docker-compose.yml
fi
if [ "$include_pgcrypto" == "y" ]; then
echo " volumes:" >> docker-compose.yml
echo " - ./initdb-pgcrypto:/docker-entrypoint-initdb.d" >> docker-compose.yml
fi
if [ "$include_timescaledb" == "y" ]; then
echo " volumes:" >> docker-compose.yml
echo " - ./initdb-timescaledb:/docker-entrypoint-initdb.d" >> docker-compose.yml
fi
# Configure additional options
echo " command: postgres -c max_connections=${max_connections} -c shared_buffers=${shared_buffers} -c work_mem=${work_mem} -c maintenance_work_mem=${maintenance_work_mem} ${custom_settings}" >> docker-compose.yml
if [ "$enable_ssl" == "y" ]; then
echo " volumes:" >> docker-compose.yml
echo " - ./ssl:/var/lib/postgresql/ssl" >> docker-compose.yml
echo " environment:" >> docker-compose.yml
echo " POSTGRES_SSL: on" >> docker-compose.yml
fi
echo " ports:" >> docker-compose.yml
echo " - 5432:5432" >> docker-compose.yml
echo " networks:" >> docker-compose.yml
echo " - mynetwork" >> docker-compose.yml
echo " healthcheck:" >> docker-compose.yml
echo " test: ["CMD", "pg_isready", "-h", "localhost", "-U", "myuser"]" >> docker-compose.yml
echo "networks:" >> docker-compose.yml
echo " mynetwork:" >> docker-compose.yml
# Provide instructions
echo ""
echo "Docker Compose configuration generated in 'docker-compose.yml'."
echo "You can customize the configuration or run 'docker-compose up'."