how to check if a database exists programatically

Started by surabhi.ahujaover 20 years ago4 messagesgeneral
Jump to latest
#1surabhi.ahuja
surabhi.ahuja@iiitb.ac.in

i have to write a shell script in which i have to check if the database "abc" exists or not

how should i do it ? some select query

select * from ??? where database_name = 'abc';

#2A. Kretschmer
andreas.kretschmer@schollglas.com
In reply to: surabhi.ahuja (#1)
Re: how to check if a database exists programatically

am 08.12.2005, um 17:25:10 +0530 mailte surabhi.ahuja folgendes:

i have to write a shell script in which i have to check if the database "abc" exists or not

how should i do it ? some select query

select * from ??? where database_name = 'abc';

You can parse the output from 'psql -l'.

HTH, Andreas
--
Andreas Kretschmer (Kontakt: siehe Header)
Heynitz: 035242/47212, D1: 0160/7141639
GnuPG-ID 0x3FFF606C http://wwwkeys.de.pgp.net
=== Schollglas Unternehmensgruppe ===

#3Devrim GÜNDÜZ
devrim@gunduz.org
In reply to: surabhi.ahuja (#1)
Re: how to check if a database exists programatically

Hi,

On Thu, 2005-12-08 at 17:25 +0530, surabhi.ahuja wrote:

i have to write a shell script in which i have to check if the
database "abc" exists or not

how should i do it ? some select query

select * from ??? where database_name = 'abc';

SELECT 1 from pg_database WHERE datname='abc';

Regards,
--
The PostgreSQL Company - Command Prompt, Inc. 1.503.667.4564
PostgreSQL Replication, Consulting, Custom Development, 24x7 support
Managed Services, Shared and Dedicated Hosting
Co-Authors: plPHP, plPerlNG - http://www.commandprompt.com/

#4Devrim GÜNDÜZ
devrim@gunduz.org
In reply to: Devrim GÜNDÜZ (#3)
Re: how to check if a database exists programatically

Hi,

On Thu, 2005-12-08 at 14:20 +0200, Devrim GUNDUZ wrote:

SELECT 1 from pg_database WHERE datname='abc';

Ah of course:

$ psql template1 -U postgres -c "SELECT 1 AS result FROM pg_database
WHERE datname='yapab'";
result
--------
1
(1 row)

$ psql template1 -U postgres -c "SELECT 1 AS result FROM pg_database
WHERE datname='abc'";

result
--------
(0 rows)

and you can parse this output too.

--
The PostgreSQL Company - Command Prompt, Inc. 1.503.667.4564
PostgreSQL Replication, Consulting, Custom Development, 24x7 support
Managed Services, Shared and Dedicated Hosting
Co-Authors: plPHP, plPerlNG - http://www.commandprompt.com/