By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,471 Members | 722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,471 IT Pros & Developers. It's quick & easy.

checking if the Database exists (by running a batch file)

P: 1
Hi,
Im v.new to postgres. I needed some help writing batch files.
I wanted to run psql commands in a batch file.
I give 2 inputs to the batch file -- Username and Database name
I want my batch file to do the following things.

- check if a database created by that user exists if true return 1 else return 0

Anyone who knows how to do this , please help !
Jul 2 '08 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 700
I'm not sure if you work under linux or windows. This is linux script that meets you requirements
Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/bin/bash
  3. psql -t -c "select * from pg_user u,pg_database d where u.usesysid=d.datdba and usename='$1' and datname='$2'" > myfile
  4. z=`wc -l myfile | cut -d" " -f 1`;
  5. if [ $z -le 1 ]
  6. then echo "0";
  7. else
  8.         echo "1";
  9. fi
  10. rm myfile
  11.  
i guess it is not perfect but i didn;t write a scipt for a long time :).
Jul 2 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.