Return-Path: mao
Received: by postgres.Berkeley.EDU (5.61/1.29)
	id AA22453; Tue, 13 Oct 92 12:52:38 -0700
Message-Id: <9210131952.AA22453@postgres.Berkeley.EDU>
From: <mao@postgres.berkeley.edu>
Subject: Re: valid database check with libpq
To: postgres@postgres.berkeley.edu
Sender: pg_adm@postgres.berkeley.edu
In-Reply-To: Your message of Tue, 13 Oct 92 11:53:59 PST.
             <9210131853.AA21906@postgres.Berkeley.EDU> 
Date: Tue, 13 Oct 92 12:55:47 PDT

In message <9210131853.AA21906@postgres.Berkeley.EDU> you write:

> I am accessing postgres using libpq.
> In my C program I get a string from the user which is supposedly the name
> of the database to access. How do I tell if this is a valid database name?

you can open the database 'template1' (which is guaranteed to be a valid
db), and the do

    sprintf(buf, "retrieve (pg_database.oid) where pg_database.datname = \"%s\"",
		user_supplied_dbname);

    result = PQexec(buf);

and then verify that you get an oid back.  after you've verified that, you
need to do PQshutdown() and then PQsetdb(user_supplied_dbname).

alternatively, you can just do PQsetdb(user_supplied_dbname), and the backend
will alert you that the database doesn't exist when you run the first query.

					mike
