Subject: Re: valid database check with libpq

From: <[email-masked]>
Date: Tue, 13 Oct 92 12:55:47 PDT

In message <[email-masked]> 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

---

From: "Patrick M. Landry" <[email-masked]>
Date: Tue, 13 Oct 92 11:53:59 -0700

--
patrick
[email-masked]