./db_incrementalbackup.sh: line 7: syntax error: unexpected "(" - postgresql

When I try to run this .sh script
#!/bin/sh
expect -c "
spawn ssh -o StrictHostKeyChecking=accept-new postgres#postgres -p 23
expect \"password: \"
send \"postgres\r\"
expect \":~$\r\"
send \"psql -U postgres -c "select * from pg_switch_wal();" -Fp\r\"
#send \"pg_basebackup -h postgres -p 5432 -U postgres -D /backup/$1 -Fp\r\"
#send \"psql -U postgres -c "select * from pg_switch_wal();" -Fp\r\"
expect \"postgres: \"
send \"postgres\r\"
expect \":~$\r\"
send \"tar -cvf /backup/$1.tar /backup/$1/ \r\"
expect \":~$\r\"
send \"rm -rf /backup/$1/ \r\"
expect \":~$\r\"
send \"exit 0\r\"
interact "
It returns the below error.
bash-5.1# ./db_incrementalbackup.sh ./db_incrementalbackup.sh: line 8: syntax error: unexpected "("
Could you please advise where I'm wrong?

Related

postgres script silently pass without any result

i am trying to execute psql queries from the bash command line passing password in following format
set PGPASSWORD=rtttttul psql -U ostgres -h localhost -d postgres -c "select * from logs" -o output.txt
Somehow my queries are not giving any results.i have tried to pass different queries or incorrect credentials but still script execute without any error.
If i don't pass password and try logging in to command prompt,everything works fine.
i want to check what basic thing i am missing above
Below command worked
PGPASSWORD=rtttttul psql -U ostgres -h localhost -d postgres -c "select * from logs" -o output.txt
remove set at start of command fixed it

syntax error at or near ":" when running parametrized query from shell

I'm trying to run a parameterized query from shell.
But when I run:
p='some stuff'
psql -d "dbname" -v v1="$p" -c "SELECT * FROM table WHERE name=:'v1'"
I'm getting the following error:
ERROR: syntax error at or near ":"
Meanwhile:
psql -d "dbname" -v v1="$p" -c "\echo :'v1'"
works normally. (returns as expected: 'some stuff')
You cannot use the variable defined in -v in -c command (see below). Try passing the command into the standard input:
psql -d "dbname" -v v1="$p" <<< "SELECT * FROM table WHERE name=:'v1'"
From the document:
-c command
--command command
...
command must be either a command string that is completely parsable by
the server (i.e., it contains no psql-specific features), or a single
backslash command.
...
-v does set the psql's internal variable, which is psql-specific features. That's why you got the syntax error.

Shell Script getting terminated in the middle when unable to make DB connection

I have a function block as below which is getting called from somewhere. The call is happening fine but when it's executing any of the below psql check commands eg:
local db_availability_check_88=`psql -h 10.95.187.88 -p 5444 -c "\pset tuples_only" -c "select pg_is_in_recovery;"|grep -v "Tuples"`
The script terminates immediately if the script is unable to communicate to the database via the above psql command. I want thee script to continue its execution. If the connectivity fails it should continue with the next statements.
check_db_status_function () {
echo "entered function block db_status"
local db_availability_check_67=`psql -h 10.95.167.87 -p 5444 -c "\pset tuples_only" -c "select pg_is_in_recovery;"|grep -v "Tuples"`
local db_availability_check_68=`psql -h 10.95.167.88 -p 5444 -c "\pset tuples_only" -c "select pg_is_in_recovery;"|grep -v "Tuples"`
local db_availability_check_69=`psql -h 10.95.167.89 -p 5444 -c "\pset tuples_only" -c "select pg_is_in_recovery;"|grep -v "Tuples"`
if [[ ( $a1 = "down" ) && ( $a3 != "primary" ) && ( $db_availability_check_67 = 't' ) ]];
then
echo "pgPool services are down on the node:$a2"
echo "Promoting..."
/u01/edb/pgpool3.6/bin/pcp_attach_node -w -U pcpuser -h localhost -p 9898 $a0
fi
The script o/p is as :
Nested block 1
check_db_stat
entered function block db_status
psql.bin: could not connect to server: Connection refused
Is the server running on host "10.95.167.88" and accepting
TCP/IP connections on port 5444?
Thanks,
Sandeep

Psql output to file with column alias starting with number

I am running psql from command line and sending output to a file. It is a simple select statement on a view, but I am getting a syntax error when I have a column alias that starts with a number.
I ran the query in PgAdmin and it works (which makes me believe that this is some sort of issue with psql). I also tried adding a '_' to the beginning of the alias and that allows it to go through.
works: 'abc as "_1abc"'
doesn't work: 'abc as "1abc"'
psql -u <username> -h <host> -p <port> -d <DB> -o <outputfile> -A -c
"SELECT abc as "1abc" From example.view
This is the error I get:
ERROR: syntax error at or near "1"
It is a problem with nested double quotes. You need to escape the inner ones.
psql -u <username> -h <host> -p <port> -d <DB> -o <outputfile> -A -c "SELECT abc as \"1abc\" From example.view"

How to use postgres user in a shell script on Ubuntu 16

I am trying to adapt a shell script set made for running on Debian 7 to work on Ubuntu 16.
I got to change successfully all except a part that executes PosgreSQL database commands.
Former version of script has these lines:
service postgresql restart
psql -q -U postgres -c "DROP DATABASE IF EXISTS db_crm" -o $log &> /dev/null
psql -q -U postgres -c "CREATE DATABASE db_crm" -o $log &> /dev/null
When I tried to run psql as above on Ubuntu 16, OS didn't recognize command. It is important to say that script is called with sudo.
I got to find a way to run only database script on Ubuntu 16 changing code so:
service postgresql restart
su postgres <<EOF
psql -c "DROP DATABASE IF EXISTS db_crm" -o $log &> /dev/null
psql -c "CREATE DATABASE db_crm" -o $log &> /dev/null
However, this same script doesn't work when it is called by main script. Following messages are presented:
here-document at line 41 delimited by end-of-file (wanted 'EOF')
syntax error: unexpected end of file
Even replacing EOF to beggining of next line, error continues.
If there is a way to use psql in shell script without to use EOF would be better.
The reason your script is failing, is you forgot the EOF at the end of input.
su postgres <<EOF
psql -c "DROP DATABASE IF EXISTS db_crm" -o $log &> /dev/null
psql -c "CREATE DATABASE db_crm" -o $log &> /dev/null
EOF #<<<--- HERE
An easy way to do this is to put your commands into a temporary file, then re-direct that into psql. Obviously you don't want this to stop for a password prompt, in this case either use a user that doesn't need it, or set $PGPASSWORD - or prompt at the beginning of the script - there's lots of ways around.
#! /usr/bin/env bash
# PGPASSWORD='' #(set this to stop password prompts, but it's insecure)
PSQL="psql -q -U postgres -o $log" #TODO define $log
TMPFILE="/tmp/sql-tmp.`date +%Y%m%d_%H%M%S_%N`.sql"
# TODO - check $TMPFILE does not exist already
echo "DROP DATABASE IF EXISTS db_crm;" > "$TMPFILE"
echo "CREATE DATABASE db_cr;" >> "$TMPFILE"
# ... etc.
# run the command, throw away any stdout, stderr
PSQL < "$TMPFILE" 2>&1 > /dev/null
# exit with the psql error code
err_code=$?
exit $?