Fastest way to check database's existence

Started by Kynn Jonesover 15 years ago5 messagesgeneral
Jump to latest
#1Kynn Jones
kynnjo@gmail.com

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?

(Maybe this question would be more suitable for the pgsql-performance list?)

TIA!

~kynn

In reply to: Kynn Jones (#1)
Re: Fastest way to check database's existence

Is there an even faster way to check for a database's existence that does
not require establishing a connection?
(Maybe this question would be more suitable for the pgsql-performance list?)

No. You have to connect to some database in particular to do anything.
That's why the postgres database exists.

--
Regards,
Peter Geoghegan

In reply to: Kynn Jones (#1)
Re: Fastest way to check database's existence

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@iol.ie

#4Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Kynn Jones (#1)
Re: Fastest way to check database's existence

On Saturday 16 October 2010 8:13:12 am 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?

Given the restriction of host/port you probably need to connect as stated in
other posts. For completeness though, it is possible to parse
$DATA/global/pg_database for the existence of database in a particular cluster.
Assuming you can map host/port to cluster then it is a possibility.

(Maybe this question would be more suitable for the pgsql-performance
list?)

TIA!

~kynn

--
Adrian Klaver
adrian.klaver@gmail.com

#5Kynn Jones
kynnjo@gmail.com
In reply to: Adrian Klaver (#4)
Re: Fastest way to check database's existence

Thank you all for your comments and suggestions!

~kj