0% found this document useful (0 votes)
29 views2 pages

Extract Data From Postgres Using Shell

This bash script automates the extraction of metadata, checksums, and full data from a PostgreSQL database. It generates SQL files from specified generator files, checks for their existence, and executes them if they are created successfully. The script also includes user prompts and waits for user input before exiting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views2 pages

Extract Data From Postgres Using Shell

This bash script automates the extraction of metadata, checksums, and full data from a PostgreSQL database. It generates SQL files from specified generator files, checks for their existence, and executes them if they are created successfully. The script also includes user prompts and waits for user input before exiting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#!

/bin/bash

# Define parameters
dbname="$1"
schema_name=$2
srvname="$3"
dbuser="$4"
dbpass="$5"
basepath=$6

export PGPASSWORD="$dbpass"
# Metadata - Rowcount extraction
echo -e "\033[1;42mCREATING METADATA SQL FILE FROM GENERATOR FILE & INITIATING
METADATA EXTRACTION\033[0m"
sleep 1
psql -h $srvname -d $dbname -U $dbuser -v schema_name="$schema_name" -v
base_path="$basepath" -f $basepath/Postgres/Input/metadata_sql_gen.sql -t -q -X -o
$basepath/Postgres/Input/metadata_sql.sql
sleep 1
echo "Checking whether the input files are created or not!"
sleep 1
# File path to check
metadata_PATH="$basepath/Postgres/Input/metadata_sql.sql"
# Check if the file exists and is not empty
if [ -f "$metadata_PATH" ] && [ -s "$metadata_PATH" ]; then
echo "Files are created in input folder. Executing metadata extract SQL
Statements..."
sleep 2
psql -h $srvname -d $dbname -U $dbuser -f
$basepath/Postgres/Input/metadata_sql.sql
else
echo "Error: $metadata_PATH is not created, check the script!"
fi

# Generate SQL statements to calculate checksums


echo -e "\033[1;42mCREATING CHECKSUM SQL FILE FROM GENERATOR FILE & INITIATING
CHECKSUM EXTRACTION\033[0m"
sleep 1
psql -h $srvname -d $dbname -U $dbuser -v schema_name="$schema_name" -v
base_path="$basepath" -f $basepath/Postgres/Input/cksum_sql_gen.sql -t -q -X -o
$basepath/Postgres/Input/cksum_sql.sql
sleep 1
echo "Checking whether the input files are created or not!"
sleep 1
# File path to check
cksum_PATH="$basepath/Postgres/Input/cksum_sql.sql"

# Check if the file exists and is not empty


if [ -f "$cksum_PATH" ] && [ -s "$cksum_PATH" ]; then
echo "Files are created in input folder. Executing checksum SQL Statements..."
sleep 2
psql -h $srvname -d $dbname -U $dbuser -f $basepath/Postgres/Input/cksum_sql.sql
else
echo "Error: $cksum_PATH is not created, check the script!"
fi
# Full Extract file gen and execution
echo -e "\033[1;42mCREATING FULL EXT SQL FILE FROM GENERATOR FILE & INITIATING FULL
EXT\033[0m"
sleep 1
psql -h $srvname -d $dbname -U $dbuser -v schema_name="$schema_name" -v
base_path="$basepath" -f $basepath/Postgres/Input/fullext_sql_gen.sql -t -q -X -o
$basepath/Postgres/Input/fullext_sql.sql
sleep 1
echo "Checking whether the input files are created or not!"
sleep 1
# File path to check
fullext_PATH="$basepath/Postgres/Input/fullext_sql.sql"

# Check if the file exists and is not empty


if [ -f "$fullext_PATH" ] && [ -s "$fullext_PATH" ]; then
echo "File are created in the input folder. Executing Full extract SQL
Statements..."
sleep 2
psql -h $srvname -d $dbname -U $dbuser -f
$basepath/Postgres/Input/fullext_sql.sql
else
echo "Error: $fullext_PATH is not created, check the script!"
fi
unset PGPASSWORD

# Print text with different colors and styles


echo "Script execution complete. Press any key to exit."

# Wait for user to press any key


read -n 1 -s

echo "Exiting..."

You might also like