title: Base58 Encoder function in PostgreSQL
body: <p>I found this MySQL function for  <a href="https://en.wikipedia.org/wiki/Base58" rel="nofollow">Base58</a> Encoder in a <a href="https://gist.github.com/micahwalter/d6c8f8bc677978cf01a5" rel="nofollow">Github Gist</a>.</p>
  
  <pre><code>DELIMITER $$
  
  CREATE FUNCTION base58_encode (num int) RETURNS varchar(255)
    DETERMINISTIC
  
  BEGIN
    DECLARE alphabet varchar(255);
    DECLARE base_count int DEFAULT 0;
    DECLARE encoded varchar(255);
    DECLARE divisor DECIMAL(10,4);
    DECLARE mode int DEFAULT 0;
  
    SET alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
    SET base_count = CHAR_LENGTH(alphabet);
    SET encoded = "";
  
    WHILE num  &gt;= base_count DO
      SET divisor = num / base_count;
      SET mode = (num - (base_count* TRUNCATE(divisor,0)));
      SET encoded = CONCAT(SUBSTRING(alphabet FROM mode+1 FOR 1), encoded);
      SET num = TRUNCATE(divisor,0);
    END WHILE;
  
    SET encoded = CONCAT(SUBSTRING(alphabet FROM num+1 FOR 1), encoded);
  
    RETURN (encoded);
  
  END
  </code></pre>
  
  <p>I am new to <code>PostgreSQL</code> and having difficulty converting above function to PostgreSQL function.</p>
  
  <p>How would be the equivalent <code>PostgreSQL</code> function of above SQL snippet for Base58 Encoder?</p>