title: How to allow only alpha-numeric field value in postgresql
body: <p>In SQL Server (T-SQL) we can do this like posted here:</p>
  
  <p><a href="http://www.sqlrelease.com/allow-only-alphanumeric-characters-in-a-column" rel="nofollow">http://www.sqlrelease.com/allow-only-alphanumeric-characters-in-a-column</a></p>
  
  <p>How to do the same in PostgreSQL?</p>
  
  <p>The following didn't worked -</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION dummy.checkalphanumeric(username character varying(32))
    RETURNS integer AS
  $BODY$
  BEGIN
  RETURN (SELECT CASE WHEN regexp_matches(username, '%[^a-zA-Z0-9]%') &gt; 
  false THEN false ELSE true END);
  END; $BODY$
    LANGUAGE plpgsql VOLATILE
    COST 100;
  ALTER FUNCTION dummy.checkalphanumeric(username character varying(32))
    OWNER TO postgres;
  </code></pre>
  
  <p>And the ALTER query -</p>
  
  <pre><code>ALTER TABLE dummy.provisioning_user ADD CONSTRAINT 
  CK_provisioning_user CHECK (dummycheckalphanumeric(username) = 1)
  </code></pre>