Thread: shell scripting

shell scripting

From
Marc Tardif
Date:
Is ther any way to foresee if a database exists or if a user has
permission to read/write a database from within a shell script?
The best I've come up with so far is to check for the exit status after
running a random command. For example:

/usr/local/pgsql/bin/pg_dump -s db > schema
if [ $? != 0 ]; then
  echo "something went wrong somewhere";
fi

Unfortunately, this is not very accurate as it still doesn't say if I have
read and/or write permissions to the database.

Please let me know if you have anything,
Marc


Re: shell scripting

From
Peter Eisentraut
Date:
On Tue, 4 Apr 2000, Marc Tardif wrote:

> Is ther any way to foresee if a database exists or if a user has
> permission to read/write a database from within a shell script?

The best (and as far as I know the only) way to determine whether you have
access to something is to try to access it. Anything else is destined for
failure some place.

Not tested, but it seems that testing like
psql -c "UPDATE foo SET a = 1 WHERE 5=6"
is sufficient and harmless. (I think psql might also return a non-zero
exit status if this fails.)


--
Peter Eisentraut                  Sernanders väg 10:115
peter_e@gmx.net                   75262 Uppsala
http://yi.org/peter-e/            Sweden


Re: shell scripting

From
Björn Lundin
Date:
Frank wrote:

> Hello,
>
> I am a PostgreSQL newbie stuck with the following question; can I write a
> unix shell script with sql statements and execute this in postgresql? In
> other databases one can use "@ /tmp/script.sql" or "start
> /path-to-script/script.sql". How can this be done in postgresql?
>
> tia,
>
> Frank
man psql

       -c, --command query
              Specifies that psql is to execute one query string,
              query,  and  then  exit.  This  is  useful in shell
              scripts.

/Björn