Обсуждение: psql shell return codes - checking if database exists

Поиск
Список
Период
Сортировка

psql shell return codes - checking if database exists

От
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?

    Mario

Re: psql shell return codes - checking if database exists

От
"Milen A. Radev"
Дата:
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

Re: psql shell return codes - checking if database exists

От
sundaram
Дата:
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-tp2088362p4488964.html
Sent from the PostgreSQL - admin mailing list archive at Nabble.com.