-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_db.sh
More file actions
95 lines (75 loc) · 2.67 KB
/
setup_db.sh
File metadata and controls
95 lines (75 loc) · 2.67 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
#!/bin/bash
set -e # Break execution on errors
# Configuration
DB_HOST="localhost"
DB_PORT="5432"
DB_USER="postgres"
DB_PASSWORD="root"
DB_NAME="l2db"
SQL_URL="https://lineage2js.github.io/scripts/l2db.sql"
SQL_FILE="l2db.sql"
# Export password for psql
export PGPASSWORD="$DB_PASSWORD"
# Full path to the SQL file
FULL_PATH="$(pwd)/$SQL_FILE"
echo "Connect to $DB_HOST:$DB_PORT..."
# Checking the connection to PostgreSQL
echo "Checking the connection to PostgreSQL..."
if ! psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres -c "SELECT 1;" >/dev/null 2>&1; then
echo "ERROR: Cannot connect to PostgreSQL server!"
echo "Please make sure that:"
echo "1. PostgreSQL is installed and running"
echo "2. Server is listening on $DB_HOST:$DB_PORT"
echo "3. User '$DB_USER' exists and password is correct"
echo ""
read -p "Press Enter to continue..."
exit 1
fi
echo "Connection to PostgreSQL successful..."
# Checking if a database exists
echo "Checking existence of database $DB_NAME..."
DB_EXISTS=$(psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres -t -A -c "SELECT 1 FROM pg_database WHERE datname = '$DB_NAME';")
if [ "$DB_EXISTS" = "1" ]; then
echo "Database $DB_NAME already exists! Exiting."
read -p "Press Enter to continue..."
exit 1
fi
echo "Database $DB_NAME does not exist, continuing..."
# Downloading SQL file
echo "Downloading SQL file from $SQL_URL..."
if ! wget -q "$SQL_URL" -O "$SQL_FILE"; then
echo "Error: Failed to download SQL file!"
read -p "Press Enter to continue..."
exit 1
fi
if [ ! -f "$FULL_PATH" ]; then
echo "Error: SQL file not found at $FULL_PATH!"
read -p "Press Enter to continue..."
exit 1
fi
echo "SQL file downloaded successfully..."
# Creating a database
echo "Creating database $DB_NAME..."
if ! psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -c "CREATE DATABASE $DB_NAME;" >/dev/null 2>&1; then
echo "Error: Failed to create database $DB_NAME!"
read -p "Press Enter to continue..."
exit 1
fi
echo "Database $DB_NAME created..."
# Importing an SQL file
echo "Importing SQL file $SQL_FILE..."
if ! psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -q -f "$FULL_PATH"; then
echo "Error: Failed to import SQL file!"
read -p "Press Enter to continue..."
exit 1
fi
echo "SQL file imported successfully..."
# List of created tables
echo ""
echo "Created tables in database $DB_NAME:"
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -t -A -c "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_name;"
# Deleting a temporary file
echo "Deleting temporary file..."
rm -f "$FULL_PATH"
echo ""
echo "=== Done! ==="