user1305398
user1305398

Reputation: 3690

Unable to run a postgresql script from bash

I am learning the shell language. I have creating a shell script whose function is to login into the DB and run a .sql file. Following are the contents of the script -

#!/bin/bash
set -x

echo "Login to postgres user for autoqa_rpt_production"
$DB_PATH -U $POSTGRESS_USER $Auto_rpt_production$TARGET_DB -p $TARGET_PORT

echo "Running SQL Dump - auto_qa_db_sync"
\\i auto_qa_db_sync.sql

After running the above script, I get the following error

./autoqa_script.sh: 39: ./autoqa_script.sh: /i: not found

Following one article, I tried reversing the slash but it didn't worked.

I don't understand why this is happening. Because when I try manually running the sql file, it works properly. Can anyone help?

Upvotes: 0

Views: 791

Answers (1)

mkf
mkf

Reputation: 710

#!/bin/bash
set -x

echo "Login to postgres user for autoqa_rpt_production and run script"
$DB_PATH -U $POSTGRESS_USER $Auto_rpt_production$TARGET_DB -p $TARGET_PORT -f auto_qa_db_sync.sql

The lines you put in a shell script are (moreless, let's say so for now) equivalent to what you would put right to the Bash prompt (the one ending with '$' or '#' if you're a root). When you execute a script (a list of commands), one command will be run after the previous terminates.

What you wanted to do is to run the client and issue a "\i ./autoqa_script.sh" comand in it.

What you did was to run the client, and after the client terminated, issue that command in Bash.

You should read about Bash pipelines - these are the way to run programs and input text inside them. Following your original idea to solving the problem, you'd write something like:

echo '\i auto_qa_db_sync.sql' | $DB_PATH -U $POSTGRESS_USER $Auto_rpt_production$TARGET_DB -p $TARGET_PORT

Hope that helps to understand.

Upvotes: 4

Related Questions