2

I want to create postgresql function (CREATE FUNCTION) with PL/Python.

How do I convert from python datetime object to postgresql timestamp object in procedure?

3
  • 1
    "postgres function in python"? Huh? You want to access postgres from python? or use python to issue a create procedure-type call to pg? Commented Jul 20, 2016 at 14:19
  • @MarcB yes, I want to create stored procedure in postgresql. Commented Jul 20, 2016 at 14:33
  • then you convert your python datetime to a format that PG accepts, and do the same in reverse with the results that PG outputs. Commented Jul 20, 2016 at 14:34

1 Answer 1

1

Tell the prepare method the type of the parameter value, a timestamp, then pass a datetime value to the execute method:

create or replace function pytest()
returns timestamp as $$

    from datetime import datetime
    plan = plpy.prepare('select $1 as my_datetime', ['timestamp'])
    rv = plpy.execute(plan, (datetime.now(),))
    return rv[0]['my_datetime']

$$ language plpythonu;

select pytest();
           pytest           
----------------------------
 2016-07-20 13:57:59.625991

Database Access from PL/Python

Sign up to request clarification or add additional context in comments.

Comments

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.