pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
psql shell return codes - checking if database exists
3+ messages / 3 participants
[nested] [flat]

* psql shell return codes - checking if database exists
@ 2008-04-15 11:15 Mario Splivalo <mario.splivalo@megafon.hr>
  2008-04-15 12:10 ` Re: psql shell return codes - checking if database exists Milen A. Radev <milen@radev.net>
  0 siblings, 1 reply; 3+ messages in thread

From: Mario Splivalo @ 2008-04-15 11:15 UTC (permalink / raw)
  To: pgsql-admin

I need to check, from a bash script, if certain database exists in a 
postgres cluster. When I issue:

psql -U someuser somedatabase

psql returns error code 2 regardless of 'no user' or 'no database' 
condition. How could I distinguish from the two?

    Mario



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: psql shell return codes - checking if database exists
  2008-04-15 11:15 psql shell return codes - checking if database exists Mario Splivalo <mario.splivalo@megafon.hr>
@ 2008-04-15 12:10 ` Milen A. Radev <milen@radev.net>
  2011-06-14 19:13   ` Re: psql shell return codes - checking if database exists sundaram <sundaram.bioreliance@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Milen A. Radev @ 2008-04-15 12:10 UTC (permalink / raw)
  To: pgsql-admin

Mario Splivalo написа:
> I need to check, from a bash script, if certain database exists in a 
> postgres cluster. When I issue:
> 
> psql -U someuser somedatabase
> 
> psql returns error code 2 regardless of 'no user' or 'no database' 
> condition. How could I distinguish from the two?

A workaround: add "--list" option to the command line and grep for the 
database you need.


-- 
Milen A. Radev




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: psql shell return codes - checking if database exists
  2008-04-15 11:15 psql shell return codes - checking if database exists Mario Splivalo <mario.splivalo@megafon.hr>
  2008-04-15 12:10 ` Re: psql shell return codes - checking if database exists Milen A. Radev <milen@radev.net>
@ 2011-06-14 19:13   ` sundaram <sundaram.bioreliance@gmail.com>
  0 siblings, 0 replies; 3+ messages in thread

From: sundaram @ 2011-06-14 19:13 UTC (permalink / raw)
  To: pgsql-admin

Perhaps the grep solution will only provide an accurate response if the
database name is not a substring of some other database name.

In other words, if one were to attempt to confirm that database prod10
exists- they might execute (per the suggestion):
psql --list | grep prod10

But, the result could be misleading if the following databases also exist on
the same server:
prod100
prod101
prod1000
myprod10
yourprod101
etc.

I would like to suggest querying directly from the system table pg_database
like this:
psql --tuples-only --command "select * from pg_database where datname =
'prod10';"

You can pipe and process the result as appropriate for your needs.

--
View this message in context: http://postgresql.1045698.n5.nabble.com/psql-shell-return-codes-checking-if-database-exists-tp208836...
Sent from the PostgreSQL - admin mailing list archive at Nabble.com.



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2011-06-14 19:13 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2008-04-15 11:15 psql shell return codes - checking if database exists Mario Splivalo <mario.splivalo@megafon.hr>
2008-04-15 12:10 ` Milen A. Radev <milen@radev.net>
2011-06-14 19:13   ` sundaram <sundaram.bioreliance@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox