From: Roberto Mello <rmello@cc.usu.edu>
To: Julester <cubalibr@optonline.net>
Cc: pgsql-sql@postgresql.org
Subject: Re: Checking for table existence
Date: Mon, 17 Sep 2001 18:12:29 -0600
Message-ID: <20010917181229.A9170@cc.usu.edu> (raw)
In-Reply-To: <prso7.50245$f7.6232115@news02.optonline.net>
References: <prso7.50245$f7.6232115@news02.optonline.net>
On Fri, Sep 14, 2001 at 06:58:29PM +0000, Julester wrote:
> Hi everyone. In my old SQL Server days, I used a command such as "IF
> exists(select name from sys_objects where name = 'xyztable')" to check if a
> table existed before creating it with a standard CREATE command. I looked
> in the PostgreSQL documentation, but for the life of me, I can't find an
> equivalent. I can view if the table exists by doing a select against the
> meta-data tables, but what about the IF statement ? Any help would be
> greatly appreciated. Thanks.
You can have psql output its internal queries and that will give you
some insight:
roberto@brasileiro:~/documents/pictures$ psql -e foobar
Welcome to psql, the PostgreSQL interactive terminal.
...
foobar=# \d blah
********* QUERY *********
SELECT relhasindex, relkind, relchecks, reltriggers, relhasrules
FROM pg_class WHERE relname='blah'
*************************
********* QUERY *********
SELECT a.attname, format_type(a.atttypid, a.atttypmod), a.attnotnull,
a.atthasdef, a.attnum
FROM pg_class c, pg_attribute a
WHERE c.relname = 'blah'
AND a.attnum > 0 AND a.attrelid = c.oid
ORDER BY a.attnum
*************************
********* QUERY *********
SELECT substring(d.adsrc for 128) FROM pg_attrdef d, pg_class c
WHERE c.relname = 'blah' AND c.oid = d.adrelid AND d.adnum = 1
*************************
Table "blah"
Attribute | Type | Modifier
-----------+--------------------------+---------------
something | timestamp with time zone | default 'now'
name | character(50)
-Roberto
--
+------------| Roberto Mello - http://www.brasileiro.net |------------+
Computer Science, Utah State University - http://www.usu.edu
USU Free Software & GNU/Linux Club - http://fslc.usu.edu
Space Dynamics Lab, Developer - http://www.sdl.usu.edu
OpenACS - Enterprise free web toolkit - http://openacs.org
Blood is thicker than water, and much tastier.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: rmello@cc.usu.edu, cubalibr@optonline.net
Subject: Re: Checking for table existence
In-Reply-To: <20010917181229.A9170@cc.usu.edu>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox