Hello Diogo dos Santos,
Thank you so much for your patience and for sharing the details, really appreciate it. I’m glad to hear your issue has been resolved. I am adding the steps that you have followed to resolve your issue.
Here's a corrected Bash script:
- Explicitly uses the right database (change DB to actual DB if required).
- Enforces sslmode=require (mandatory on Azure).
- Surfaces errors by removing quiet flags when installing functions.
- Logs properly, with least-privilege file permissions
#!/bin/bash
# Paths to log files
LOG_OK="/appl/mydir/logs/success_change_password.log"
LOG_ERR="/appl/mydir/logs/error_change_password.log"
# Clear log files at the beginning of the script and add a timestamp
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo "### SCRIPT EXECUTION LOG FOR PASSWORD CHANGE FROM $timestamp" > "$LOG_OK"
echo "### ERROR LOG FOR SCRIPT EXECUTION OF PASSWORD CHANGE FROM $timestamp" > "$LOG_ERR"
# Function to install pg functions into the database
run_sql_commands() {
local user=$1
local pass=$2
local id=$3
local host=$4
local port=$5
local login=$6
local password=$7
local dbname="postgres"
# Install first function
PGPASSWORD="$pass" /usr/bin/psql "host=$host port=$port user=$user dbname=$dbname sslmode=require" \
-v ON_ERROR_STOP=1 -f /appl/mydir/first_function.sql >> "$LOG_OK" 2>> "$LOG_ERR" || return 1
# Install second function
PGPASSWORD="$pass" /usr/bin/psql "host=$host port=$port user=$user dbname=$dbname sslmode=require" \
-v ON_ERROR_STOP=1 -f /appl/mydir/second_function.sql >> "$LOG_OK" 2>> "$LOG_ERR" || return 1
# Call function and capture output
update_sql="SELECT second_function($id, '$login', '$password');"
update_var=$(PGPASSWORD="$pass" /usr/bin/psql "host=$host port=$port user=$user dbname=$dbname sslmode=require" \
-AXqtc "$update_sql" 2>> "$LOG_ERR") || return 1
echo "$update_var" > /appl/mydir/update_string.sql
chmod 640 /appl/mydir/update_string.sql
return 0
}
# Read input arguments line by line
while read -r i j k l m; do
id="$i"
host="$j"
port="$k"
login="$l"
password="$m"
# Load user info and password for DB connection from .env
while IFS='=' read -r key value; do
case "$key" in
"USER2") user="$value" ;;
"PASSWORD") pass="$value" ;;
"USER4") user2="$value" ;;
"PASSWORD4") pass2="$value" ;;
esac
done < /appl/mydir2/.env
if [ -n "$user" ] && [ -n "$pass" ]; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Connecting to host: $host, port: $port with USER2" >> "$LOG_OK"
if run_sql_commands "$user" "$pass" "$id" "$host" "$port" "$login" "$password"; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Connection to host: $host, port: $port with USER2 was successful" >> "$LOG_OK"
elif [ -n "$user2" ] && [ -n "$pass2" ]; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Connecting to host: $host, port: $port with USER4" >> "$LOG_OK"
run_sql_commands "$user2" "$pass2" "$id" "$host" "$port" "$login" "$password"
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Failure to connect to host: $host, port: $port" >> "$LOG_ERR"
fi
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')] User or password not found in .env file" >> "$LOG_ERR"
fi
done < /appl/mydir/var_select.txt
# Clean up
rm -f "/appl/mydir/var_select.txt" 2>> "$LOG_ERR"
rm -f "/appl/mydir/update_string.sql" 2>> "$LOG_ERR"
Connect manually to the target database:
psql -h <host> -U <user> -d <Target db>
- Check if functions are installed
- Review
$LOG_ERR
for any suppressed errors.
Could you please try running the script once and let me know if you still face any issues? I’ll be happy to help further. If you find this information helpful, kindly consider clicking Upvote so it can assist others as well.
Thanks,
Kalyani