[GENERAL] Check if table exists
Hi ALL! I need to check before selection records from table - does this table exist How can i do that? Thanx in advance ---(end of broadcast)--- TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED]
Re: [GENERAL] Check if table exists
ON.KG wrote: Hi ALL! I need to check before selection records from table - does this table exist How can i do that? You could: 1. Look in the INFORMATION_SCHEMA 2. Check pg_class 3. Examine the output of psql -E, \d to see how psql gets its information. -- Richard Huxton Archonet Ltd ---(end of broadcast)--- TIP 8: explain analyze is your friend
Re: [GENERAL] Check if table exists
--- ON.KG __ wrote: Hi ALL! I need to check before selection records from table - does this table exist How can i do that? Thanx in advance ---(end of broadcast)--- TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED] Option 1: tells you if the table exists in your database select * from pg_catalog.pg_tables as t where t.schemaname = 'your schema name' and t.tablename = 'your table name'; Option 2: tells you if the table exists in your database and if is visible in your search path (i.e. no need to select * from schema.table, only select * from table) select * from pg_catalog.pg_class as c left outer join pg_catalog.pg_namespace as n on n.oid = c.relnamespace where n.nspname = 'your schema name' and c.relname = 'your table name' and pg_catalog.pg_table_is_visible(c.oid); ---(end of broadcast)--- TIP 7: don't forget to increase your free space map settings
Re: [GENERAL] Check if table exists
Sorry: I forgot to add something to option 2 --- ON.KG __ wrote: Hi ALL! I need to check before selection records from table - does this table exist How can i do that? Thanx in advance ---(end of broadcast)--- TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED] Option 1: tells you if the table exists in your database select * from pg_catalog.pg_tables as t where t.schemaname = 'your schema name' and t.tablename = 'your table name'; Option 2: tells you if the table exists in your database and if is visible in your search path (i.e. no need to select * from schema.table, only select * from table) select * from pg_catalog.pg_class as c left outer join pg_catalog.pg_namespace as n on n.oid = c.relnamespace where n.nspname = 'your schema name' and c.relname = 'your table name' and c.relkind = 'r' and pg_catalog.pg_table_is_visible(c.oid); ---(end of broadcast)--- TIP 4: Don't 'kill -9' the postmaster
Re: [GENERAL] Check if table exists
Richard Huxton Riccardo G. Facchini Thank you very much! ---(end of broadcast)--- TIP 6: Have you searched our list archives? http://archives.postgresql.org
Re: [GENERAL] Check if table exists
ON.KG wrote: Hi ALL! I need to check before selection records from table - does this table exist How can i do that? One thing to consider: you are making a trip to the database to determine if a table exists. If it exists, you are then making another trip to the database to get rows from it. For a non-destructive operation (i.e., select), you may be better off just doing the select, and watching for a table does not exist error. -- Guy Rouillier ---(end of broadcast)--- TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED]