How to check if a table exists

Started by Jeff Luabout 25 years ago4 messagesgeneral
Jump to latest
#1Jeff Lu
jklcom@mindspring.com

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

#2Ben Leslie
benno@sesgroup.net
In reply to: Jeff Lu (#1)
Re: How to check if a table exists

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

#3Tim Barnard
tbarnard@povn.com
In reply to: Jeff Lu (#1)
Re: 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

Show quoted text

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)

#4Tim Barnard
tbarnard@povn.com
In reply to: Tim Barnard (#3)
Re: How to check if a table exists

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

Show quoted text

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)