Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Newbie
 
Join Date: Jul 2008
Posts: 1
#1: Jul 2 '08
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 !

Moderator
 
Join Date: Dec 2006
Location: Europe
Posts: 292
#2: Jul 2 '08

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


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 :).
Reply