Cannot truncate a table using psycopg2

Viewed 5442

I try to truncate a table from a Python application using psycopg2. The operation returns -1 without either exception or any indication of a problem. The table is not truncated. The statement itself is correct as it works when I run it via psql.

Both DB server and client are RHEL 7.6. I use PostgreSQL 11.2, Python 2.75 and psycopg2 2.5.1.3el7

The code is very simple:

m_dbCon = psycopg2.connect(user=dbUser, password=dbPasswd, host=dbHost, port=dbPort, database=dbInstName)
curr = m_dbCon.cursor()
curr.execute('truncate table my_table')
count = curr.rowcount
curr.close()

As I've said no exception is thrown and the count is -1.

I could not find any explanation for this issue.

What am I doing wrong?

2 Answers

As Jeremy stated in the comments, rowcount should be ignored in case of TRUNCATE.

I believe that absence of an exception means success of the statement . At least, it is so in this case.

Related