DEV Community

PGzlan
PGzlan

Posted on

A little script for PostgreSQL and AGE

While trying to install PostgreSQL and Apache AGE, I noticed a lot of routine steps that could be automated. So I made this following script to automate the build and install of both PostgreSQL and Apache AGE.
Please note that you have to check for version compatibility for both applications (may add a check for that later)

#!/bin/bash

set -e

echo "Retrieving listings..."
listings=$(curl -s https://www.postgresql.org/ftp/source/ | grep -Po '(?<=href=")[^"]*' | grep '^v')

echo "Available listings:"
count=1
while read -r listing; do
  echo "$count. $listing"
  ((count++))
done <<< "$listings"

read -p "Please enter the number of the listing you'd like to explore: " listing_number

selected_listing=$(echo "$listings" | sed -n "${listing_number}p")

echo "Retrieving files for listing $selected_listing..."
files=$(curl -s "https://www.postgresql.org/ftp/source/$selected_listing" | grep -Po '(?<=href=")[^"]*')

echo "Available files:"
count=1
while read -r file; do
  echo "$count. $file"
  ((count++))
done <<< "$files"

read -p "Please enter the number of the file you'd like to download: " file_number

selected_file=$(echo "$files" | sed -n "${file_number}p")

echo "Downloading file $selected_file..."
wget "https://www.postgresql.org/ftp/source/$selected_listing$selected_file"

echo "File downloaded successfully!"

echo "Creating directories..."
mkdir -p age_installation/pg
cd age_installation/pg

echo "Installing dependencies..."
sudo apt-get install -y build-essential libreadline-dev zlib1g-dev flex bison
sudo apt-get install -y postgresql-server-dev-11

echo "Extracting PostgreSQL source..."
tar -xf "../$selected_file"
source_folder=$(tar -tf "../$selected_file" | head -1 | cut -f1 -d"/")

cd "$source_folder"

echo "Configuring PostgreSQL..."
./configure --enable-debug --enable-cassert --prefix=$(pwd) CFLAGS="-ggdb -O0 -fno-omit-frame-pointer"

echo "Building and installing PostgreSQL..."
make world
make install-world

cd ../../age/

echo "Installing age..."
sudo make PG_CONFIG=$(pwd)/../pg/$source_folder/bin/pg_config install

echo "Running age install check..."
make PG_CONFIG=$(pwd)/../pg/$source_folder/bin/pg_config installcheck

cd "../$source_folder"

echo "Initializing PostgreSQL demo database..."
bin/initdb demo

echo "Starting PostgreSQL server..."
bin/pg_ctl -D demo -l logfile start

echo "Creating demodb..."
bin/createdb demodb

echo "Accessing demodb..."
bin/psql demodb
Enter fullscreen mode Exit fullscreen mode

Happy sql'ing!

Top comments (0)