2

I have successfully setup plpyton3u extension in Postgresql 10 (64 bit) on my windows 10 (64 bit) machine. However, when i try to make a http request by calling requests module I am getting attribute error AttributeError: 'module' object has no attribute 'get' . Here is the code that I am using

CREATE OR REPLACE FUNCTION from_url(
-- The URL to download.
IN url text,
-- Should any errors (like HTTP transport errors)
-- throw an exception or simply return default_response
IN should_throw boolean DEFAULT true,
-- The default response if any errors are found.
-- Only used when should_throw is set to true
IN default_response text DEFAULT E''
)
    RETURNS text
AS $$
    # We will use traceback so we get decent error reporting.
    import requests
    import traceback

    # Either throws an error or returns the defeault response
    # depending on the should_throw parameter of the from_url() function
    def on_error():
        if should_throw:
            # plpy.error() throws an exception which stops the current transaction
            plpy.error("Error downloading '{0}'\n {1}".format(url, traceback.format_exc()))
        else:
            return default_response
    try:
        response = requests.get(url)
        return response.data
    except:
        # Log and re-throw the error or return the default response
        return on_error()


$$ LANGUAGE plpython3u VOLATILE;

select from_url(<SOME_DATA_FETCHING_URL>);

Where SOME_DATA_FETCHING_URL is the url of the server serving the data. When I run this code it throws following error

ERROR: plpy.Error: Error downloading SOME_DATA_FETCHING_URL Traceback (most recent call last): File "", line 16, in __plpython_procedure_from_url_24640 AttributeError: 'module' object has no attribute 'get'

CONTEXT: Traceback (most recent call last): PL/Python function "from_url", line 19, in return on_error() PL/Python function "from_url", line 11, in on_error plpy.error("Error downloading '{0}'\n {1}".format(url, traceback.format_exc())) PL/Python function "from_url" SQL state: XX000

my PYTHONPATH is set to C:\Python34\;C:\Python34\Scripts;C:\Python34\Lib;

I checked on python command prompt, I am able to import requests and run the get command successfully.

Am I missing any settings in PostGRESQL? which will allow me to run this code successfully?

which_part
  • 792
  • 3
  • 11
  • 26

1 Answers1

0

(In case anyone else stumbles on this question) I had a similar problem, in my case it turned out to be caused by inadequate access permissions for user 'postgres' to the directory containing my Python modules. One thing I have learnt is that to check a PL/Python problem using the Python command prompt, it's always necessary to run it as the same user as the Postgres server (in my case, 'postgres')