Installing and adding Python to your PostgreSQL database in Ubuntu:

sudo apt-get install postgresql-plpython-9.1
sudo -u postgres psql -c 'CREATE EXTENSION plpythonu;' dbname

Check that it was installed:

psql -c '\dL' dbname
       List of languages
   Name    |  Owner   | Trusted 
 plpgsql   | postgres | t 
 plpythonu | postgres | f 
(2 rows)

Keep in mind that Python is an ‘untrusted’ language meaning that functions written in PL/Pythonu execute in an administrative context. For this reason, Python functions can only be created by an administrator and special care should be taken that nothing damaging or nefarious can be done with the function by non administrator users of the database.

Now, you can declare a function in Python as an administrator.

CREATE FUNCTION shuffle(arg text) RETURNS text AS
  import random
  a = list(arg)
  return ''.join(a)
LANGUAGE plpythonu;
=> SELECT shuffle('foo bar');
 oa forb
(1 row)