1

apart from checking if table already exists, how can we verify if it is current user-defined table in postgres

CREATE TABLE IF NOT EXISTS tbl1()

how can we verify it was created by current user if already exists?

1 Answer 1

2

Try Like This

 SELECT count(*) as cnt FROM pg_tables t where tableowner=current_user 
and tablename='Tablename'  and schemaname='schemaneme'

if cnt = 0 then (CREATE Table TableName)
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for reply, it works with pg_tales and current_user. Can we use it for multiple tables, if we have hundred tables in one script by having table list or we have to wrap every table with it?
give the table name in where clause Use In get the tables name and below check the condition using for loop
sorry i could not get you, could you explain again?
tablename='Tablename' change to tablename in (tablenames). count(*)` Change to tablename into rec. Rec as record. now use for loop for create table

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.