Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL 9.3: IF NOT EXISTS [duplicate]

I want to check whether the table exists or not in the database.

IF NOT EXISTS (SELECT * from INFORMATION_SCHEMA.Tables WHERE Table_name = 'test') THEN

    RAISE INFO 'Not exists';

else

    RAISE INFO 'Exists';

end if;

Getting an error:

ERROR:  syntax error at or near "IF"
like image 329
MAK Avatar asked Feb 25 '15 06:02

MAK


1 Answers

DO
$do$
BEGIN
IF NOT EXISTS (SELECT * from INFORMATION_SCHEMA.Tables WHERE Table_name = 'test') THEN

    RAISE INFO 'Not exists';

else

    RAISE INFO 'Exists';

end if;
end;
$do$

You should surround your postgresql statements with block

like image 88
DAlekperov Avatar answered Nov 06 '22 00:11

DAlekperov