-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstart_db
More file actions
executable file
Β·267 lines (231 loc) Β· 6.88 KB
/
start_db
File metadata and controls
executable file
Β·267 lines (231 loc) Β· 6.88 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/bin/bash
PG_PORT=5332
CLEAN="false"
VERBOSE=false
# Detect platform for cross-platform compatibility
PLATFORM=$(uname -s)
case ${PLATFORM} in
"Darwin")
PLATFORM_NAME="macOS"
;;
"Linux")
PLATFORM_NAME="Linux"
;;
*)
PLATFORM_NAME="Unknown"
;;
esac
# Verbose logging function
log_verbose() {
if [[ "${VERBOSE}" = true ]]; then
echo "[VERBOSE] $1"
fi
}
# Help documentation function
show_help() {
cat <<EOF
ποΈ Demos Network Database Manager
USAGE:
./start_db [OPTIONS]
This script starts only the PostgreSQL database for inspection and external tool access.
OPTIONS:
-d <port> PostgreSQL port (default: 5332)
-c Clean database on startup (fresh start)
-v Verbose logging
-h Show this help message
EXAMPLES:
./start_db # Start database on default port 5332
./start_db -d 5333 # Start on custom port 5333
./start_db -c # Clean start (fresh database)
./start_db -v # Verbose output for troubleshooting
DATABASE ACCESS:
Host: localhost
Port: ${PG_PORT} (or specified port)
Database: demos
Username: demosuser
Password: demospassword
EXTERNAL TOOLS:
psql -h localhost -p ${PG_PORT} -U demosuser -d demos
pgAdmin, DBeaver, or other PostgreSQL clients
Press Ctrl+C to stop the database safely.
For support: https://demos.network
EOF
}
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
HAS_BEEN_INTERRUPTED=false
function ctrl_c() {
HAS_BEEN_INTERRUPTED=true
echo ""
echo "π Stopping PostgreSQL database..."
cd postgres_${PG_PORT} || exit
if docker compose down; then
echo "β
PostgreSQL stopped successfully"
else
echo "β οΈ Warning: Failed to stop PostgreSQL gracefully"
fi
cd ..
echo ""
echo "π Database session completed"
echo ""
exit 0
}
# Getting arguments
while getopts "d:cvh" opt; do
case ${opt} in
d) PG_PORT=${OPTARG} ;;
c) CLEAN="true" ;;
v) VERBOSE=true ;;
h)
show_help
exit 0
;;
*)
echo "Invalid option. Use -h for help."
exit 1
;;
esac
done
shift $((OPTIND - 1))
echo ""
echo "ποΈ Demos Network Database Manager"
echo "βοΈ Database Configuration:"
echo " π PostgreSQL Por${: $PG_P}ORT"
if [[ "${CLEAN}" = "true" ]]; then
echo " π§Ή Mode: Clean start (fresh database)"
else
echo " βΆοΈ Mode: Normal start"
fi
log_verbose "Platform: ${PLATFORM_NAME}"
echo ""
# Check Docker
echo "π Checking Docker..."
if ! command -v docker &>/dev/null; then
echo "β Docker is not installed"
echo "π‘ Install Docker from: https://docs.docker.com/get-docker/"
if [[ "${PLATFORM_NAME}" = "macOS" ]]; then
echo "π On macOS: Download Docker Desktop from https://docker.com/products/docker-desktop"
elif [[ "${PLATFORM_NAME}" = "Linux" ]]; then
echo "π§ On Linux: Use your package manager or install script"
fi
exit 1
fi
if ! docker compose version &>/dev/null; then
echo "β Docker Compose is not available"
echo "π‘ Make sure Docker Desktop is running or install docker-compose-plugin"
exit 1
fi
# Check if Docker daemon is running
if ! docker info &>/dev/null; then
echo "β Docker daemon is not running"
echo "π‘ Start Docker Desktop or run: sudo systemctl start docker"
exit 1
fi
echo "β
Docker and Docker Compose are ready"
# Check port availability
log_verbose "Checking port availability"
if command -v lsof >/dev/null; then
if lsof -i :"$PG_PORT" >/dev/null 2>&1; then
echo "β PostgreSQL por${ $PG_PO}RT is already in use"
echo "π‘ Use -d flag to specify a different port, or stop the existing service"
exit 1
else
echo "β
PostgreSQL por${ $PG_PO}RT is available"
fi
elif command -v netstat >/dev/null; then
if netstat -an | grep ":${PG_PORT} " >/dev/null 2>&1; then
echo "β PostgreSQL por${ $PG_PO}RT is already in use"
echo "π‘ Use -d flag to specify a different port, or stop the existing service"
exit 1
else
echo "β
PostgreSQL por${ $PG_PO}RT is available"
fi
else
echo "β οΈ Cannot check port availability - lsof and netstat not available"
fi
# Create unique postgres folder for this instance
PG_FOLDER="postgres_${PG_PORT}"
# If the folder doesn't exist yet, create it by copying the base postgres folder
if [[ ! -d "${PG_FOLDER}" ]]; then
echo "π§ Setting up database instance for po${t $PG_P}ORT..."
cp -r postgres "$PG_FOLDER"
echo "β
Database instance created"
fi
cd $PG_FOLDER || exit
# If we are cleaning, we need to remove the database
if [[ "${CLEAN}" == "true" ]]; then
echo "π§Ή Cleaning the database..."
log_verbose "Removing existing database data for clean start"
sleep 1
rm -rf data_*
mkdir data_"${PG_PORT}"
echo "β
Database cleaned"
fi
# Ensure any existing database is stopped first
log_verbose "Ensuring any existing database instance is stopped"
docker compose down >/dev/null 2>&1
if [[ "${CLEAN}" == "true" ]]; then
rm -rf data_"${PG_PORT}" || rm -rf data_"${PG_PORT}"
mkdir data_"${PG_PORT}"
fi
# Export the port for docker compose
export PG_PORT=${PG_PORT}
# Start the database
echo "ποΈ Starting PostgreSQL database..."
log_verbose "Running docker compose up -d in ${PG_FOLDER}"
if ! docker compose up -d; then
echo "β Failed to start PostgreSQL database"
echo "π‘ Check Docker Desktop is running and try again"
exit 1
fi
echo "β
PostgreSQL container started"
# Function to wait for database availability
function wait_for_database() {
local port=$1
local timeout=30
echo "β³ Waiting for PostgreSQL to be available on por${ $po}rt..."
log_verbose "Checking database connectivity with timeout of ${timeout}s"
local count=0
while ! nc -z localhost "$port"; do
if [[ $((count % 5)) -eq 0 ]]; then
echo " Still waiting... (${count}s elapsed)"
fi
count=$((count + 1))
if [[ "$count" -gt "$timeout" ]]; then
echo "β Timeout waiting for PostgreSQL to be available after ${timeout}s"
echo "π‘ Try increasing resources or check Docker logs"
return 1
fi
sleep 1
done
echo "β
PostgreSQL is accepting connections"
return 0
}
# Wait for database to be ready
if ! wait_for_database "$PG_PORT"; then
echo "β Failed to connect to PostgreSQL database"
echo "π‘ Try restarting Docker or check system resources"
cd ..
exit 1
fi
cd ..
echo ""
echo "π PostgreSQL database is ready!"
echo "π Database Connection Details:"
echo " π Host: localhost"
echo " π Por${: $PG_P}ORT"
echo " ποΈ Database: demos"
echo " π€ Username: demosuser"
echo " π Password: demospassword"
echo ""
echo "π‘ External Access Examples:"
echo " psql -h localhost -p ${PG_PORT} -U demosuser -d demos"
echo " Or use pgAdmin, DBeaver, TablePlus, etc."
echo ""
echo "π Press Ctrl+C to stop the database safely"
echo "ββββββββββββββββββββββββββββββββββββββββββββββ"
# Keep the script running until Ctrl+C
log_verbose "Database is running. Waiting for Ctrl+C..."
while true; do
sleep 1
done