Re: Fastest way to check database's existence

From: Raymond O'Donnell <rod(at)iol(dot)ie>
To: Kynn Jones <kynnjo(at)gmail(dot)com>
Cc: pgsql-general General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Fastest way to check database's existence
Date: 2010-10-16 15:41:43
Message-ID: 4CB9C7B7.7000304@iol.ie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 16/10/2010 16:13, Kynn Jones wrote:
> I want to code a Perl function (part of a Perl library) for determining
> the existence of a particular database (in a given host/port).
>
> One way would be to just attempt making a connection to it, trapping any
> errors upon failure (with eval), or discarding the connection upon success.
>
> This approach has the added benefit of also checking the accessibility
> of the database to the user running the code, but for this application
> this added benefit is not necessary. Checking existence is all that
> matters.
>
> Is there an even faster way to check for a database's existence that
> does not require establishing a connection?

You're going to have to connect no matter what you do, assuming that
you're accessing it from another machine.

An alternative, if it suited your application, would be to maintain a
connection to a database which you know exists, such as template1 or
(better) postgres, and just query pg_database for the existence of the
database you want. If you can keep a connection open for long periods,
I'm sure this would be pretty fast.

Ray.

--
Raymond O'Donnell :: Galway :: Ireland
rod(at)iol(dot)ie

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Terry Laurenzo 2010-10-16 15:58:30 Re: Request for comment: pgjson project
Previous Message Peter Geoghegan 2010-10-16 15:39:13 Re: Fastest way to check database's existence