Re: create table if does not exists

From: Chris <dmagick(at)gmail(dot)com>
To: Yan Cheng Cheok <yccheok(at)yahoo(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: create table if does not exists
Date: 2010-01-08 02:22:55
Message-ID: 4B4696FF.9080402@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Yan Cheng Cheok wrote:
> I try to have the following stored procedure, to help me create tables, if the table does not exists (Is this a good practice by the way?)
>
>
> if not exists(select * from information_schema.tables where table_name = 'MYTABLE') then
>
> RAISE NOTICE 'table not there yet.';
>
> CREATE TABLE MYTABLE
> (
> "value" text NOT NULL
> );
>
> end if;
>
>
> When I run for the 2nd time, 'table not there yet.' still being printed out, al though I check through pgadmin, the MYTABLE is there.
>
> May I know how I can fix this?

The table will be named "mytable" rather than 'MYTABLE'.

Postgres lowercases names (tables, fields etc) unless you put them in
quotes ("LikeThis") - but then you always have to quote them like that.

If you change your check to be

where table_name='mytable'

it should get picked up correctly.

--
Postgresql & php tutorials
http://www.designmagick.com/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Craig Ringer 2010-01-08 03:21:13 Re: Minimizing disk space
Previous Message Yan Cheng Cheok 2010-01-08 02:10:04 create table if does not exists