URL-encoding VARCHAR and TEXT values in PostgreSQL

3.5k views Asked by At

Are there any well-known PL functions/libraries for extending a PostgreSQL (9.4.1) database with URL encoding (also known as percent encoding) capabilities?

Here's an example of the intended functionality:

  • Input string: International donor day: give blood for a good cause!
  • Output string: International%20donor%20day%3A%20give%20blood%20for%20a%20good%20cause%21

I guess an alternative would be to roll out my own implementation, since AFAIK there is currently no built-in way of doing this.

1

There are 1 answers

1
Craig Ringer On BEST ANSWER

This is trivial to do in an external PL,e.g.

CREATE LANGUAGE plpythonu;

CREATE OR REPLACE FUNCTION urlescape(original text) RETURNS text LANGUAGE plpythonu AS $$
import urllib
return urllib.quote(original);
$$
IMMUTABLE STRICT;