Re: pl sql to check if table of table_name exists - Mailing list pgsql-general

From Sim Zacks
Subject Re: pl sql to check if table of table_name exists
Date
Msg-id d0pfn6$bg6$1@news.hub.org
Whole thread Raw
In response to Re: pl sql to check if table of table_name exists  (Shaun Clements <ShaunC@relyant.co.za>)
List pgsql-general
I'm glad to hear you got it working.
In explanation to my response:
the pg_class internal table lists all the relationships in the database.
relkind='r' means that the relation you are looking for is a table (relation), I believe that will also find views.
relname is the name of the object if your table is called parts
select * from pg_class where relkind='r' and relname='parts' will give you the pg_class record for the table if it exists and nothing if it doesn't.
you could also do a select count(*) or select 1 In any case if there is a resultset the table exists and if there is no resultset the the table does not.
Using the pg_tables view is a better idea in any case, as it is cleaner.
 
Sim
Hi Sim
 
Thanks for your response. I had it working from a previous post by Adam Tomjack.
<snip>

-- A list of tables:

SELECT schemaname, tablename FROM pg_tables;

-- Returns true if a table exists:

SELECT count(*)>0 FROM pg_tables

WHERE schemaname='...' AND tablename='...'

</snip>
 
 
Your response does not work for me. Perhaps you can explain the posted command
 
<snip>
* from pg_class where relkind='r' and relname=your_tablename
</snip>
 
A {FONT-FAMILY: verdana; TEXT-DECORATION: none } A:active {COLOR: #ff0000; FONT-FAMILY: verdana; TEXT-DECORATION: none } A:link {COLOR: #003366; FONT-FAMILY: verdana; TEXT-DECORATION: none } A:visited {COLOR: #003366; FONT-FAMILY: verdana; TEXT-DECORATION: none } A:hover {COLOR: #ff0000; FONT-FAMILY: verdana; TEXT-DECORATION: underline } .small_text {FONT-SIZE: 9px; COLOR: #003366; FONT-FAMILY: verdana } Kind Regards,
Shaun Clements
 
 
 -----Original Message-----
From: Sim Zacks [mailto:sim@compulab.co.il]
Sent: 10 March 2005 01:24 PM
To: pgsql-general@postgresql.org
Subject: Re: [GENERAL] pl sql to check if table of table_name exists

i mean
select * from pg_class where relkind='r' and relname=your_tablename
"Sim Zacks" <sim@compulab.co.il> wrote in message news:d0pamh$2l83$1@news.hub.org...
select your_tablename from pg_class where relkind='r'

Hi

Hate to ask, but it isnt obvious to me from the documentation.
How do I perform a query in pgplsql, to check it a table exists of a particular name.

Thanks in advance

Kind Regards,
Shaun Clements

pgsql-general by date:

Previous
From: Martijn van Oosterhout
Date:
Subject: Re: normal user dump gives error because of plpgsql
Next
From: Shaun Clements
Date:
Subject: Re: pl sql to check if table of table_name exists