Register
It is currently Fri Jul 25, 2014 5:22 am

Getting bash to run several psql scripts


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Tue Nov 03, 2009 5:13 am   

Joined: Tue Nov 03, 2009 5:08 am
Posts: 1
Hi,
(Total newbie bash scripting question.)
Ubuntu 9.10, GNU bash, version 4.0.33(1)-release (i486-pc-linux-gnu)

I have a heap of psql scripts that I need to run unattended every night.
As far as I can see, there would be two methods for doing this with bash.

Method 1 is to have all the scripts concatenated in one big file and get cron to run it.
(I have done this and it's working.)

Method 2 would be to leave each script in its own file and get bash to run each script.
I would prefer to use Method 2, but I don't know how to set it up.

I have the idea OK, (Cf. the pseudo code below,) but not the syntax.

Any help appreciated.
TIA
Vronth


--- Method 1 ----- All scripts concatenated ------------------

bin/bash
psql

## Script 1. orders
select this, that, the_other from orders
where x y and z
group by this
order by that ;

## Script 2. customer bills
select this, that, the_other from customer_bills
where x y and z
group by this
order by that ;

## Script 3. customer_bills and orders stats
select this, that, the_other from customer_bills, orders
where x y and z
group by this
order by that ;

exit psql

--- Method 2 -------- Each script separate ---------------------

bin/bash
psql

## Script 1. orders
run psql 'input file orders.script'

## Script 2. customer bills
run psql 'input file customer bills.script'

## Script 3. customer_bills and orders stats
run psql 'input file customer_bills and orders stats.script'

exit psql

-------------------------------------------------------------------


Top
 Profile  
 PostPosted: Tue Nov 03, 2009 6:40 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
I'm not very good with PSQL. But it should work in the same manor MySQL does.
My normal way of running SQL questions against a MySQL from bash is to either create a .sql file with the query or use HERE DOC.

Using HERE DOC
Code:
# psql <<EOF
SELECT
   *
FROM
   tblname;
EOF

You can put this all on one row also, the formating isn't vital, what is vital is the EOF on it's own row.
This can work too, it's the same basically
Code:
# psql <<< "SELECT * FROM tblname;"


Using SQL file
Code:
# psql < inputfile.sql


using this in a script can look something like this:
Code:
#!/bin/bash
/path/to/psql <<EOF
select this, that, the_other from orders
where x y and z
group by this
order by that ;
EOF

/path/to/psql <<EOF
select this, that, the_other from customer_bills
where x y and z
group by this
order by that ;
EOF

/path/to/psql <<EOF
select this, that, the_other from customer_bills, orders
where x y and z
group by this
order by that ;
EOF


Best regards
Fredrik Eriksson


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP