DO
$$
BEGIN
IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='<TABLENAME>' and column_name='c_karty') THEN

	IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='<TABLENAME>' and column_name='c_karty' and udt_name<>'int4') THEN
		ALTER TABLE <SCHEMA>.<TABLENAME> ALTER COLUMN c_karty TYPE int4 ;
	END IF;

	IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='<TABLENAME>' and column_name='c_karty' and lower(btrim(is_nullable))<>'no') THEN
                UPDATE      <SCHEMA>.<TABLENAME> SET c_karty = 0 WHERE c_karty IS NULL;
		ALTER TABLE <SCHEMA>.<TABLENAME> ALTER COLUMN c_karty SET NOT NULL  ;
	END IF;

	IF EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='<SCHEMA>' and table_name='<TABLENAME>' and column_name='c_karty' and (lower(btrim(column_default))<>'0' or column_default is null) ) THEN
		ALTER TABLE <SCHEMA>.<TABLENAME> ALTER COLUMN c_karty SET DEFAULT 0 ;
	END IF;

 END IF;
END
$$
