Re: Create table if not exists ... how ??

From: "Igor Neyman" <ineyman(at)perceptron(dot)com>
To: "Joe Conway" <mail(at)joeconway(dot)com>, "Jennifer Trey" <jennifer(dot)trey(at)gmail(dot)com>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Create table if not exists ... how ??
Date: 2010-07-19 20:54:26
Message-ID: F4C27E77F7A33E4CA98C19A9DC6722A2064560C5@EXCHANGE.corp.perceptron.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> -----Original Message-----
> From: Joe Conway [mailto:mail(at)joeconway(dot)com]
> Sent: Monday, July 19, 2010 12:59 PM
> To: Jennifer Trey
> Cc: pgsql-general(at)postgresql(dot)org
> Subject: Re: Create table if not exists ... how ??
>
> On 07/19/2010 09:33 AM, Jennifer Trey wrote:
> > I can't figure out the correct syntax...
> >
> > I have this, but it just keeps complaining about the IF
> >
> > IF NOT EXISTS (SELECT table_name FROM
> information_schema.tables where
> > table_name = 'post_codes')
> >
> > THEN
> >
> > CREATE TABLE post_codes
>
> Probably better to do:
>
> DROP TABLE IF EXISTS post_codes;
> CREATE TABLE post_codes(...);
>
> See:
> http://www.postgresql.org/docs/8.4/interactive/sql-droptable.html
>
> HTH,
>
> Joe
>
> --
> Joe Conway
> credativ LLC: http://www.credativ.us
> Linux, PostgreSQL, and general Open Source Training, Service,
> Consulting, & 24x7 Support
>

Joe,

What you suggest is completely different from what OP asked.

Jen wants to avoid getting error on CREATE TABLE in case her table
already exists (but proceed with CREATE TABLE, if it doesn't).
What you suggest, will drop the table (IF EXISTS), and then create it
"anew" - what if there is already data in the table?

Regards,
Igor Neyman

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Joe Conway 2010-07-19 20:55:38 Re: Create table if not exists ... how ??
Previous Message Michael Friedrich 2010-07-19 20:22:12 Re: NASA needs Postgres - Nagios help