Skip to content

DO

DO
$do$
BEGIN
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'test') THEN
RAISE NOTICE 'Table exists.';
ELSE
RAISE NOTICE 'Table does not exist.';
END IF;
END
$do$;
NOTICE: Table does not exist.

The DO statement in PostgreSQL executes an anonymous code block, which does not need to be stored in the database. The example statement checks if a table named test exists in the database. If it exists, it outputs the message ‘Table exists.’, otherwise it outputs ‘Table does not exist.’.