Home > Software engineering >  How to alter decimal scale and precision in psycopg2 using python?
How to alter decimal scale and precision in psycopg2 using python?

Time:12-04

I am using the script that contains the code below

    query = """ALTER TABLE table_name ALTER COLUMN column_name NUMERIC (22,6);"""
    
    self.cursor.execute(query)
    self.connection.commit()

but i get error below

psycopg2.errors.SyntaxError: syntax error at or near "NUMERIC"

Have you encountered error like this? I have searched nothing is out on the internet.

PS: I have used DECIMAL instead of Numeric, same error appears

CodePudding user response:

If you look at the PostgreSQL documentation for ALTER TABLE, you'll see you need to use the TYPE keyword (mandatory):

ALTER [ COLUMN ] column_name [ SET DATA ] TYPE data_type [ COLLATE collation ]
      [ USING expression ]
ALTER TABLE table_name ALTER COLUMN column_name TYPE NUMERIC (22,6)

Also, this error shows that this is a syntax error coming from PostgreSQL. Practically, your question has nothing to do with Python or Psycopg2:

psycopg2.errors.SyntaxError: syntax error at or near "NUMERIC"
  • Related