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

From Steve Atkins
Subject Re: Create table if not exists ... how ??
Date
Msg-id FD3B66B0-64FD-43C3-9F6E-1A8A6868DC48@blighty.com
Whole thread Raw
In response to Re: Create table if not exists ... how ??  (Jennifer Trey <jennifer.trey@gmail.com>)
List 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
notthe next time. 
>
> Should I just let Java throw and exception and catch it ? Write a function for this would be optimal, although I have
noidea 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


pgsql-general by date:

Previous
From: Vick Khera
Date:
Subject: Re: Inheritance and trigger/FK propagation
Next
From: Said Ramirez
Date:
Subject: Re: Create table if not exists ... how ??