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

From: Raymond O'Donnell <rod(at)iol(dot)ie>
To: 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 16:39:12
Message-ID: 4C447FB0.20902@iol.ie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 19/07/2010 17:33, 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
> (
> area character varying(10) NOT NULL,
> district character varying(10) NOT NULL,
> sector character varying(10) NOT NULL,
> CONSTRAINT post_codes_pkey PRIMARY KEY (area, district, sector)
> )
> WITH (
> OIDS=FALSE
> );
> ALTER TABLE post_codes OWNER TO postgres;
>
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> - - - - - - - - - - - -
>
> ERROR: syntax error at or near "IF"
> LINE 1: IF NOT EXISTS (SELECT table_name FROM information_schema.tab...
> ^

> How should this be written ?

I don't think you can use the "IF" like this in a normal query. You
could write a pl/pgsql function instead to do this..

Ray.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Pavel Stehule 2010-07-19 16:40:51 Re: Create table if not exists ... how ??
Previous Message Jennifer Trey 2010-07-19 16:33:39 Create table if not exists ... how ??