0

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.

3
  • Are you aware that it is possible to use some languages like Python: plpython? Commented Jun 10, 2015 at 17:14
  • @ClodoaldoNeto: indeed - I was just thinking that maybe there's a popular/universally accepted set of functions out there that I could use straight out of the box. I suppose that if I don't come across anything better, I will indeed have to write something myself (preferably using a high level PL like plpython). Commented Jun 10, 2015 at 19:56
  • 1
    URL encoding is a one liner in Python. Commented Jun 10, 2015 at 20:21

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That's exactly what I needed. Note to anyone else reading this: In Python 3 the module hierarchy has been redesigned, so for plpython3u functions, use urllib.parse.quote(original) instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.