Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

psycopg2 does not execute PostgreSQL function

I'm trying to call a function from psycopg2 like such:

conn = psycopg2.connect(host="name.host.ex", user="username", password="secret")
cur = conn.cursor()
cur.callproc("f_do_action", ["aaa", "bbb"])
cur.close()
conn.close()

When calling this function from psql everything works fine but using psycopg2 nothing seems to happen. I get no exception. It just does not call out the function on the actual database.

Also other queries from psycopg2 work (SELECT, INSERT).

like image 915
Rob Fox Avatar asked Nov 13 '11 11:11

Rob Fox


People also ask

Does psycopg2 work with python3?

Many Python types are supported out-of-the-box and adapted to matching PostgreSQL data types; adaptation can be extended and customized thanks to a flexible objects adaptation system. Psycopg 2 is both Unicode and Python 3 friendly.

What is the difference between procedure and function in PostgreSQL?

In Postgres, the main functional difference between a function and a stored procedure is that a function returns a result, whereas a stored procedure does not. This is because the intention behind a stored procedure is to perform some sort of activity and then finish, which would then return control to the caller.


1 Answers

Try committing before closing your connection:

cur.close()
conn.commit()
conn.close()

From psycopg2 documentation:

Note that closing a connection without committing the changes first will cause any pending change to be discarded as if a ROLLBACK was performed (unless a different isolation level has been selected: see set_isolation_level()).

like image 151
Pablo Santa Cruz Avatar answered Sep 24 '22 21:09

Pablo Santa Cruz