On Thu, Oct 29, 2015 at 02:42:00AM +0000, Tim Landscheidt wrote:
> Hi,
> 
> I regularly run into the problem that I want to query a
> PostgreSQL database in a script/program and depending on a
> boolean result do one thing or the other.  A typical example
> would be a Puppet Exec that creates a user only if it does
> not exist yet.
> 
> But unfortunately psql always returns with the exit code 0
> if the query was run without errors.  In a shell script I
> can use a query that returns an empty string for failure and
> something else for success and then test that à la:


<snip>

What I do is use grep, for example (off the top of my head):

if ! psql -qAt -c "select usename from pg_user" | grep -q USERNAME ;
then
...

If you're looking for true/false you could grep for t/f.

Hope this helps,
-- 
Martijn van Oosterhout   <klep...@svana.org>   http://svana.org/kleptog/
> The combine: one man, one day, wheat for half a million loaves of bread.

Attachment: signature.asc
Description: Digital signature

Reply via email to