create or replace function create_virtual_device_table() returns void as $$ begin if not exists(select * from information_schema.tables where table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA and table_name = 'VirtualDevice') then CREATE TABLE "VirtualDevice" ( "ID" serial NOT NULL, "ChamberID" text, "DeviceName" character varying, "State" integer, "LastModifyDate" timestamp without time zone, "FunctionBlock" text, CONSTRAINT "VirtualDevice_pkey" PRIMARY KEY ("ID" ) ) WITH ( OIDS=TRUE ); ALTER TABLE "VirtualDevice" OWNER TO postgres; GRANT ALL ON TABLE "VirtualDevice" TO postgres; GRANT ALL ON TABLE "VirtualDevice" TO promaxyuser; end if; end; $$ language 'plpgsql'; select create_virtual_device_table();