Repository navigation
Expand file tree
/
Copy pathsetup-postgres.sh
More file actions
108 lines (100 loc) · 3.44 KB
/
Copy pathsetup-postgres.sh
File metadata and controls
108 lines (100 loc) · 3.44 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
#!/bin/bash
# Setup PostgreSQL Docker container for OpenTron
# Run this script to start PostgreSQL and configure the environment
set -e
echo "=========================================="
echo "OpenTron PostgreSQL Docker Setup"
echo "=========================================="
echo ""
# Check if Docker is running
echo "[1/5] Checking Docker daemon..."
if ! docker ps > /dev/null 2>&1; then
echo "❌ Docker daemon is not running!"
echo " Please start Docker Desktop and try again."
exit 1
fi
echo "✅ Docker daemon is running"
echo ""
# Check if container already exists
echo "[2/5] Checking for existing PostgreSQL container..."
if docker ps -a --format '{{.Names}}' | grep -q "^opentron-postgres$"; then
echo "⚠️ Container 'opentron-postgres' already exists"
if docker ps --format '{{.Names}}' | grep -q "^opentron-postgres$"; then
echo "✅ Container is already running"
CONTAINER_RUNNING=true
else
echo " Starting existing container..."
docker start opentron-postgres
echo "✅ Container started"
CONTAINER_RUNNING=true
fi
else
echo " Creating new PostgreSQL container..."
docker run -d \
--name opentron-postgres \
-e POSTGRES_DB=opentron \
-e POSTGRES_USER=opentron \
-e POSTGRES_PASSWORD=opentron_secure_password \
-p 5432:5432 \
-v postgres_data:/var/lib/postgresql/data \
postgres:16-alpine
echo "✅ PostgreSQL container created"
CONTAINER_RUNNING=true
fi
echo ""
# Wait for PostgreSQL to be ready
echo "[3/5] Waiting for PostgreSQL to be ready..."
for i in {1..30}; do
if docker exec opentron-postgres pg_isready -U opentron > /dev/null 2>&1; then
echo "✅ PostgreSQL is ready"
break
fi
if [ $i -eq 30 ]; then
echo "❌ PostgreSQL failed to start within 30 seconds"
exit 1
fi
echo " Waiting... ($i/30)"
sleep 1
done
echo ""
# Verify connection
echo "[4/5] Verifying connection..."
if docker exec opentron-postgres psql -U opentron -d opentron -c "SELECT version();" > /dev/null 2>&1; then
VERSION=$(docker exec opentron-postgres psql -U opentron -d opentron -c "SELECT version();" -t)
echo "✅ Connection successful"
echo " PostgreSQL: $VERSION"
else
echo "❌ Connection failed"
exit 1
fi
echo ""
# Display connection information
echo "[5/5] Configuration Summary"
echo "=========================================="
echo "✅ PostgreSQL is ready!"
echo ""
echo "Connection Details:"
echo " Host: localhost"
echo " Port: 5432"
echo " Database: opentron"
echo " User: opentron"
echo " Password: opentron_secure_password"
echo ""
echo "Docker Container:"
echo " Name: opentron-postgres"
echo " Image: postgres:16-alpine"
echo " Status: Running"
echo ""
echo "Environment Variables (set these for your Java backend):"
echo " export POSTGRES_URL=jdbc:postgresql://localhost:5432/opentron"
echo " export POSTGRES_USER=opentron"
echo " export POSTGRES_PASSWORD=opentron_secure_password"
echo ""
echo "Useful Docker Commands:"
echo " docker logs opentron-postgres # View logs"
echo " docker exec -it opentron-postgres psql -U opentron -d opentron # Connect via psql"
echo " docker stop opentron-postgres # Stop container"
echo " docker start opentron-postgres # Start container"
echo " docker rm opentron-postgres # Remove container"
echo ""
echo "=========================================="