How to abort psql when backtick (shell) commands return non-zero?

From: david_e_roche(at)yahoo(dot)com (David Roche)
To: pgsql-general(at)postgresql(dot)org
Subject: How to abort psql when backtick (shell) commands return non-zero?
Date: 2004-05-05 00:06:55
Message-ID: 66e0ff31.0405041606.6b56f639@posting.google.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello,

I searched all through Google Groups, Google, and the Postgres docs, but
to no avail. I hope someone can help me out here!

I have a file that contains SQL, and some Postgres-specific slash commands.
This file calls an external shell script via the backtick mechanism, and
saves the value into a psql variable, for latter insertion into a table. It
looks something like this:

\set password '\'' `/dir/genPass user1` '\''

...

INSERT INTO TEST (password) VALUES (:password);

However, in some error scenarios, the /dir/genPass application fails, and
returns non-zero. However, in these cases, the stderr of the application
just gets stored in the variable, and hence inserted into my table as the
password. This is bad, and causes trouble latter... :)

BTW, I launch my "sql script" like so:

psql MY_DB < populatePasswords.sql

Question: how can I cause my populatePasswords.sql script to fail with an
error if the /dir/genPass command returns an error? I'm okay with any sort
of failure, as long as populatePasswords.sql stops executing and never gets
to the INSERT statement. It would also be nice if some sort of error would
print to the user ... ideally the stderr from the shell command (genPass).

Thank you for your time,
David Roche

Browse pgsql-general by date

  From Date Subject
Next Message Adam Witney 2004-05-05 10:02:55 Is Linux 2.6.5 kernel good enough for production?
Previous Message Bruno Wolff III 2004-05-04 22:42:42 Re: interval output format available that removes ambiguity ?