Skip to content

Instantly share code, notes, and snippets.

@rdeguzman
Created December 30, 2011 05:28
Show Gist options
  • Save rdeguzman/1538064 to your computer and use it in GitHub Desktop.
Save rdeguzman/1538064 to your computer and use it in GitHub Desktop.
Calling a python module from postgres function
CREATE FUNCTION pymax (a integer, b integer)
RETURNS integer
AS $$
if a > b:
return a
return b
$$ LANGUAGE plpythonu;
SELECT pymax(1,2) #=> 2
CREATE TYPE google_lon_lat AS (lon numeric, lat numeric);
CREATE FUNCTION google_geocode(param_address text) RETURNS
google_lon_lat
AS
$$
from googlemaps import GoogleMaps
gmaps = GoogleMaps()
arg_lat, arg_long = gmaps.address_to_latlng(param_address)
return (arg_long, arg_lat)
$$
language 'plpythonu';
SELECT google_geocode('2 Birkenhead Ave, Wantirna, VIC, 3152')
ERROR: ImportError: No module named googlemaps
CONTEXT: Traceback (most recent call last):
PL/Python function "google_geocode", line 2, in <module>
from googlemaps import GoogleMaps
PL/Python function "google_geocode"
********** Error **********
ERROR: ImportError: No module named googlemaps
SQL state: XX000
Context: Traceback (most recent call last):
PL/Python function "google_geocode", line 2, in <module>
from googlemaps import GoogleMaps
PL/Python function "google_geocode"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment