Re: Create table if not exists ... how ?? - Mailing list pgsql-general

From Jennifer Trey
Subject Re: Create table if not exists ... how ??
Date
Msg-id AANLkTilT5TL3lqv_g22V7cxI9emg4BFyj3v8-s1D7shw@mail.gmail.com
Whole thread Raw
In response to Re: Create table if not exists ... how ??  (Joe Conway <mail@joeconway.com>)
Responses Re: Create table if not exists ... how ??  (Steve Atkins <steve@blighty.com>)
Re: Create table if not exists ... how ??  (Said Ramirez <sramirez@vonage.com>)
Re: Create table if not exists ... how ??  (Joe Conway <mail@joeconway.com>)
List pgsql-general
No.... I don't want to drop it ... there is valuable data in there! I only want to create it if it doesn't already exist... likely going to happen first time the application will run. I want to create the table then and populate. But not the next time.

Should I just let Java throw and exception and catch it ? Write a function for this would be optimal, although I have no idea what the correct syntax is.

Cheers, Jen



On Mon, Jul 19, 2010 at 5:58 PM, Joe Conway <mail@joeconway.com> wrote:
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


pgsql-general by date:

Previous
From: "Duncavage, Daniel P. (JSC-OD211)"
Date:
Subject: Re: NASA needs Postgres - Nagios help
Next
From: Vick Khera
Date:
Subject: Re: Inheritance and trigger/FK propagation