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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deepikabg
    New Member
    • Jul 2008
    • 1

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

    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 !
  • rski
    Recognized Expert Contributor
    • Dec 2006
    • 700

    #2
    I'm not sure if you work under linux or windows. This is linux script that meets you requirements
    Code:
    #!/bin/bash
    psql -t -c "select * from pg_user u,pg_database d where u.usesysid=d.datdba and usename='$1' and datname='$2'" > myfile
    z=`wc -l myfile | cut -d" " -f 1`;
    if [ $z -le 1 ]
    then echo "0";
    else
            echo "1";
    fi
    rm myfile
    i guess it is not perfect but i didn;t write a scipt for a long time :).

    Comment

    Working...