-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.ConfigureDb.sh
executable file
·84 lines (74 loc) · 3.12 KB
/
4.ConfigureDb.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
#!/bin/sh
echo "======================================"
echo " Step 1: Configuring PostgreSQL Database"
echo "======================================"
# Step 1: Check if PostgreSQL service is running
echo "1. Checking PostgreSQL service status..."
sudo systemctl is-active --quiet postgresql.service
if [ $? -ne 0 ]; then
echo "✅ PostgreSQL service is not running. Starting the service..."
sudo systemctl start postgresql.service
if [ $? -ne 0 ]; then
echo "❌ Failed to start PostgreSQL service. Exiting."
exit 1
else
echo "✅ PostgreSQL service started successfully."
fi
else
echo "✅ PostgreSQL service is already running."
fi
# Step 2: Check if the database already exists
echo "2. Checking if the database plugin_mainnet_db exists..."
DB_EXISTS=$(sudo -i -u postgres psql -tAc "SELECT 1 FROM pg_database WHERE datname='plugin_mainnet_db';")
if [ "$DB_EXISTS" = "1" ]; then
echo "✅ Database plugin_mainnet_db already exists. Skipping database creation."
else
# Step 3: Generate a strong password for the database
echo "3. Generating a secure password for the database..."
DB_PASSWORD=$(openssl rand -base64 24)
# Ensure the password length is at least 16 characters
while [ ${#DB_PASSWORD} -lt 16 ]; do
DB_PASSWORD=$(openssl rand -base64 24)
done
echo "✅ Password generated successfully: $DB_PASSWORD"
# Step 4: Save the password to a file called dbpasswordkey.txt
echo "4. Saving password to dbpasswordkey.txt..."
echo "$DB_PASSWORD" > dbpasswordkey.txt
if [ $? -ne 0 ]; then
echo "❌ Failed to save the password to dbpasswordkey.txt. Exiting."
exit 1
else
echo "✅ Password saved to dbpasswordkey.txt."
fi
# Step 5: Login to PostgreSQL as the postgres user and create the database
echo "5. Creating the database plugin_mainnet_db..."
sudo -i -u postgres psql -c "CREATE DATABASE plugin_mainnet_db;"
if [ $? -ne 0 ]; then
echo "❌ Failed to create database plugin_mainnet_db. Exiting."
exit 1
else
echo "✅ Database plugin_mainnet_db created successfully."
fi
fi
# Step 6: Check if the password for the postgres user needs to be set
echo "6. Checking if the postgres user password needs to be set..."
USER_PASSWORD_SET=$(sudo -i -u postgres psql -tAc "SELECT 1 FROM pg_shadow WHERE usename='postgres' AND passwd IS NOT NULL;")
if [ "$USER_PASSWORD_SET" = "1" ]; then
echo "✅ Password for postgres user is already set. Skipping password update."
else
# Step 7: Alter the PostgreSQL user password
echo "7. Setting the password for the postgres user..."
sudo -i -u postgres psql -c "ALTER USER postgres WITH PASSWORD '$DB_PASSWORD';"
if [ $? -ne 0 ]; then
echo "❌ Failed to set the password for the postgres user. Exiting."
exit 1
else
echo "✅ Password set for the postgres user."
fi
fi
# Step 8: Exit PostgreSQL
echo "8. Exiting PostgreSQL..."
sudo -i -u postgres psql -c "\q"
echo "======================================"
echo "✅ PostgreSQL database configured successfully!"
echo "======================================"