title: How to cast string value to enum
body: <p>I have a table with an <code>enum</code> type in it, and I created a function to add data to that table. I want that function to be generous in what to accept, so I take a <code>text</code> as the enum type and want to cast it later.</p>
  
  <p>This is the enum:</p>
  
  <pre><code>CREATE TYPE public.enum_log_priority AS ENUM (
      'critical','error','warning','notice','debug'
  );
  </code></pre>
  
  <p>And this is the function:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION public.log_write(
      _message text,
      _priority text
  ) RETURNS integer AS
  $body$
  BEGIN
      _priority = lower(_priority);
      INSERT INTO log (message, priority) VALUES (_message, _priority);
  
      RETURN 0;
  END
  $body$
  LANGUAGE 'plpgsql';
  </code></pre>
  
  <p>I know this doesn't work: </p>
  
  <blockquote>
    <p>ERROR:  column "priority" is of type enum_log_priority but expression is of type text</p>
  </blockquote>
  
  <p>but how can I do this?</p>