Skip to content Skip to sidebar Skip to footer

Error When Trying To Insert Values In Postgres Query Inside Bash Script

I'm executing the query as follows: ssh user@XX.XX.1XX.XX 'PGPASSWORD=myPassword psql -U psqlUser -h XX.XX.XX.XX -p 5432 -d myDB -c 'INSERT INTO table(\'CPU_IDLE_TIME\',\'TOTAL_SIZ

Solution 1:

Use printf to format the string for you

ssh user@XX.XX.1XX.XX "PGPASSWORD=myPassword psql -U psqlUser -h XX.XX.XX.XX -p 5432 -d myDB -c ""$(printf 'INSERT INTO table("CPU_IDLE_TIME","TOTAL_SIZE","USED_SIZE","USED_STORAGE_P") VALUES (%d, %d, %d, %d);' $idlecputime $totalSize $usedSize $usedStoragePercentage)"

The $( ) construct executes printf in a subshell.

Post a Comment for "Error When Trying To Insert Values In Postgres Query Inside Bash Script"