Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old July 2nd, 2008, 06:19 AM
Newbie
 
Join Date: Jul 2008
Posts: 1
Default 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 !
Reply
  #2  
Old July 2nd, 2008, 09:54 PM
Moderator
 
Join Date: Dec 2006
Posts: 250
Default

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

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles