Thread: How to check if a table exists

How to check if a table exists

From
"Jeff"
Date:
Also how to programmatically check if a table exists in the database?



Re: How to check if a table exists

From
Ben Leslie
Date:
On Wed, 07 Mar 2001, Jeff wrote:

> Also how to programmatically check if a table exists in the database?

I think the easiest way is to attach to one of the default database like
"Template0" i think, then query the system tables. pg_database and
pg_class are theones you will be after i think.

(Well that how I've done it.. there is probably a better way ;)

Cheers,

Benno

Re: How to check if a table exists

From
"Tim Barnard"
Date:
I do it this way using libpq: select count(*) from pg_class where
relname='table name goes here'
and test for a count > 1.

Tim

----- Original Message -----
From: "Jeff" <jklcom@mindspring.com>
To: <pgsql-general@postgresql.org>
Sent: Wednesday, March 07, 2001 8:48 PM
Subject: [GENERAL] How to check if a table exists


> Also how to programmatically check if a table exists in the database?
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
>     (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
>


Re: How to check if a table exists

From
"Tim Barnard"
Date:
Test result of PQexec() using PQresultstatus(). On success you'll get a
PGRES_TUPLES_OK result.
Your subsequent call to PQfname() will contain the word "count" and a
similar call to PQgetvalue() will contain count's value, as a string of
course. If count = 0, table missing. If count = 1, table is present.

Checkout the source for psql to see how to use aforementioned functions.

Have fun ;-)

Tim

----- Original Message -----
From: "Jeff Lu" <jklcom@mindspring.com>
To: "Tim Barnard" <tbarnard@povn.com>
Sent: Thursday, March 08, 2001 8:25 PM
Subject: RE: [GENERAL] How to check if a table exists


> Thanks Tim,
>
> What's the syntax for testing count?  Is count a variable?
>
> Thanks
> -Jeff
>
> -----Original Message-----
> From: Tim Barnard [mailto:tbarnard@povn.com]
> Sent: Thursday, March 08, 2001 11:16 AM
> To: Jeff
> Cc: pgsql-general@postgresql.org
> Subject: Re: [GENERAL] How to check if a table exists
>
>
> I do it this way using libpq: select count(*) from pg_class where
> relname='table name goes here'
> and test for a count > 1.
>
> Tim
>
> ----- Original Message -----
> From: "Jeff" <jklcom@mindspring.com>
> To: <pgsql-general@postgresql.org>
> Sent: Wednesday, March 07, 2001 8:48 PM
> Subject: [GENERAL] How to check if a table exists
>
>
> > Also how to programmatically check if a table exists in the database?
> >
> >
> >
> > ---------------------------(end of broadcast)---------------------------
> > TIP 2: you can get off all lists at once with the unregister command
> >     (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
> >
>
>