|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +#============================================================================== |
| 4 | +# pg_fasttransfer Installation Wizard |
| 5 | +#============================================================================== |
| 6 | +# This script automatically detects your PostgreSQL installation and installs |
| 7 | +# the pg_fasttransfer extension. |
| 8 | + |
| 9 | +# Define the extension file names. The .dll is replaced by .so on Linux. |
| 10 | +SO_FILE="pg_fasttransfer.so" |
| 11 | +CONTROL_FILE="pg_fasttransfer.control" |
| 12 | +SQL_FILE="pg_fasttransfer--1.0.sql" |
| 13 | + |
| 14 | +echo "" |
| 15 | +echo "==========================================================" |
| 16 | +echo " pg_fasttransfer Linux Installation Wizard" |
| 17 | +echo "==========================================================" |
| 18 | +echo "" |
| 19 | +echo "This script will find your PostgreSQL installation and copy the necessary files." |
| 20 | +echo "You will be asked for your password to use 'sudo' for file copying." |
| 21 | +echo "" |
| 22 | + |
| 23 | +# Function to find the pg_config executable |
| 24 | +find_pg_config() { |
| 25 | + echo "Searching for pg_config..." |
| 26 | + # Check if pg_config is in the system's PATH |
| 27 | + if command -v pg_config &> /dev/null; then |
| 28 | + PG_CONFIG_PATH=$(command -v pg_config) |
| 29 | + echo "Found pg_config at: $PG_CONFIG_PATH" |
| 30 | + else |
| 31 | + echo "Error: pg_config was not found in your system's PATH." |
| 32 | + echo "Please provide the full path to your PostgreSQL 'bin' directory" |
| 33 | + echo "(e.g., /usr/lib/postgresql/16/bin):" |
| 34 | + read -p "PostgreSQL BIN path: " USER_BIN_PATH |
| 35 | + |
| 36 | + if [ -x "$USER_BIN_PATH/pg_config" ]; then |
| 37 | + PG_CONFIG_PATH="$USER_BIN_PATH/pg_config" |
| 38 | + echo "Using specified pg_config at: $PG_CONFIG_PATH" |
| 39 | + else |
| 40 | + echo "Error: The specified path does not contain a valid pg_config executable." |
| 41 | + exit 1 |
| 42 | + fi |
| 43 | + fi |
| 44 | +} |
| 45 | + |
| 46 | +# Find the correct installation directories using pg_config |
| 47 | +get_pg_paths() { |
| 48 | + echo "" |
| 49 | + echo "Determining PostgreSQL installation paths..." |
| 50 | + LIB_PATH=$("$PG_CONFIG_PATH" --pkglibdir) |
| 51 | + SHARE_PATH=$("$PG_CONFIG_PATH" --sharedir)/extension |
| 52 | + echo "Library directory: $LIB_PATH" |
| 53 | + echo "Extension directory: $SHARE_PATH" |
| 54 | +} |
| 55 | + |
| 56 | +# Function to check if the extension files already exist |
| 57 | +check_existing_files() { |
| 58 | + echo "" |
| 59 | + echo "Checking for existing extension files..." |
| 60 | + FILES_EXIST=false |
| 61 | + if [ -f "$LIB_PATH/$SO_FILE" ] && [ -f "$SHARE_PATH/$CONTROL_FILE" ] && [ -f "$SHARE_PATH/$SQL_FILE" ]; then |
| 62 | + FILES_EXIST=true |
| 63 | + fi |
| 64 | + |
| 65 | + if [ "$FILES_EXIST" = true ]; then |
| 66 | + echo "Existing files found. This will be an update." |
| 67 | + else |
| 68 | + echo "No existing files found. This will be a new installation." |
| 69 | + fi |
| 70 | +} |
| 71 | + |
| 72 | +# Function to check for a running PostgreSQL service |
| 73 | +check_service_status() { |
| 74 | + echo "" |
| 75 | + echo "Verifying PostgreSQL service status..." |
| 76 | + if pgrep -x "postgres" > /dev/null; then |
| 77 | + POSTGRES_RUNNING=true |
| 78 | + echo "A PostgreSQL process is currently running." |
| 79 | + else |
| 80 | + POSTGRES_RUNNING=false |
| 81 | + echo "No PostgreSQL process is running." |
| 82 | + fi |
| 83 | +} |
| 84 | + |
| 85 | +# --- Main Script Logic --- |
| 86 | +find_pg_config |
| 87 | +get_pg_paths |
| 88 | +check_existing_files |
| 89 | +check_service_status |
| 90 | + |
| 91 | +# Safety check: if updating and service is running, abort. |
| 92 | +if [ "$FILES_EXIST" = true ] && [ "$POSTGRES_RUNNING" = true ]; then |
| 93 | + echo "" |
| 94 | + echo "==================== INSTALLATION FAILED ====================" |
| 95 | + echo "" |
| 96 | + echo "The PostgreSQL service is currently running AND the extension files already exist." |
| 97 | + echo "To avoid corruption during an update, you MUST stop the service first." |
| 98 | + echo "" |
| 99 | + echo "Action required:" |
| 100 | + echo "1. Stop the PostgreSQL service (e.g., 'sudo systemctl stop postgresql')." |
| 101 | + echo "2. Rerun this script." |
| 102 | + exit 1 |
| 103 | +fi |
| 104 | + |
| 105 | +# Copy the files using sudo |
| 106 | +echo "" |
| 107 | +echo "Copying files..." |
| 108 | +sudo cp "$SO_FILE" "$LIB_PATH" |
| 109 | +if [ $? -ne 0 ]; then |
| 110 | + echo "Error copying '$SO_FILE'. Check permissions." |
| 111 | + exit 1 |
| 112 | +fi |
| 113 | + |
| 114 | +sudo cp "$CONTROL_FILE" "$SHARE_PATH" |
| 115 | +if [ $? -ne 0 ]; then |
| 116 | + echo "Error copying '$CONTROL_FILE'. Check permissions." |
| 117 | + exit 1 |
| 118 | +fi |
| 119 | + |
| 120 | +sudo cp "$SQL_FILE" "$SHARE_PATH" |
| 121 | +if [ $? -ne 0 ]; then |
| 122 | + echo "Error copying '$SQL_FILE'. Check permissions." |
| 123 | + exit 1 |
| 124 | +fi |
| 125 | + |
| 126 | +echo "" |
| 127 | +echo "==========================================================" |
| 128 | +echo " Installation completed successfully!" |
| 129 | +echo "==========================================================" |
| 130 | +echo "" |
| 131 | +echo "To finalize the installation:" |
| 132 | +echo "1. Restart your PostgreSQL service (e.g., 'sudo systemctl restart postgresql')." |
| 133 | +echo "2. Then, connect to psql and execute:" |
| 134 | + |
| 135 | +if [ "$FILES_EXIST" = true ]; then |
| 136 | + echo " DROP EXTENSION pg_fasttransfer;" |
| 137 | + echo " CREATE EXTENSION pg_fasttransfer CASCADE;" |
| 138 | +else |
| 139 | + echo " CREATE EXTENSION pg_fasttransfer CASCADE;" |
| 140 | +fi |
| 141 | + |
| 142 | +echo "" |
| 143 | +exit 0 |
0 commit comments