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

From: Steve Atkins <steve(at)blighty(dot)com>
To: PostgreSQL List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Create table if not exists ... how ??
Date: 2010-07-19 17:57:30
Message-ID: FD3B66B0-64FD-43C3-9F6E-1A8A6868DC48@blighty.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Jul 19, 2010, at 10:43 AM, Jennifer Trey wrote:

> 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

Try something like this:

create or replace function build_foo_table() returns void as $$
create table foo (bar int);
$$ language sql;

select case when (select count(*) from information_schema.tables where table_name='foo')=0 then build_foo_table() end;

drop function build_foo_table();

Cheers,
Steve

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Joe Conway 2010-07-19 18:05:21 Re: Create table if not exists ... how ??
Previous Message Vick Khera 2010-07-19 17:56:55 Re: Inheritance and trigger/FK propagation