0

I have the statement create table if not exists so that my code does not try to create the table over and over. I would like to print a message of "Table already created" if the table already exists but I don't know how to access the "notice" spoken of in the documentation so I can use it in a conditional statement.

From documentation:

IF NOT EXISTS
Do not throw an error if a relation with the same name already exists. A notice is issued in this case. Note that there is no guarantee that the existing relation is anything like the one that would have been created.

Erwin Brandstetter
  • 539,169
  • 125
  • 977
  • 1,137

1 Answers1

0

To raise your own NOTICE / WARNING / EXCEPTION, take a different approach. Like:

DO
$do$
BEGIN
   IF EXISTS (SELECT FROM pg_catalog.pg_tables 
              WHERE  schemaname = 'myschema'
              AND    tablename  = 'mytable') THEN
      RAISE NOTICE 'Table myschema.mytable already created.';
   ELSE
      CREATE TABLE myschema.mytable (i integer);  -- your table definition here
   END IF;
END
$do$;

See:

Erwin Brandstetter
  • 539,169
  • 125
  • 977
  • 1,137