0

I am trying to make a conditional Select statement that creates a table if the table does not exist, but this does not seem to work for some reason?

IF SELECT to_regclass('public.entityName') IS NULL
BEGIN
    CREATE TABLE |entityName| 
        (....)
END
END IF
2
  • 1
    Why not simply use create table if not exists? Commented Mar 10, 2020 at 19:37
  • @a_horse_with_no_name what I ended up doing :) Commented Mar 11, 2020 at 14:21

2 Answers 2

0

You could use this to return true/false:

SELECT EXISTS 
(
  SELECT 1 
  FROM pg_tables
  WHERE schemaname = 'schema'
  AND tablename = 'table'
);

Hope it helps

Sign up to request clarification or add additional context in comments.

6 Comments

I am looking for a table not a database... @ChechoCZ
Is your table in camel Case? Like 'myTable'? In that case, you should wrap the table name in double quotes. Have you tried it?
it is lower case... i just want to see if it there and if not create it
Kafka, I'll edit the answer with the solution. Hope it helps...
I need to trigger something if does not exist? how do i do that then? @ChechoCZ
|
0

I ended doing thins

CREATE TABLE IF NOT EXISTS public.name
    (...);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.