Search code examples
sqlpostgresqlddlalter-table

ERROR: syntax error at or near "modify" - in postgres


I executed this SQL statement in Postgres

alter table user modify column 'distinguishedName1' text;

and

alter table user modify column distinguishedName1 text;
  • user is the table name
  • distinguishedName1 is the column name with integer data type.

I wanted to modify the data type to boolean or text or varchar(256) etc based on user's input. But when I run the query I get the error

ERROR: syntax error at or near "modify"

Not sure what is the problem. Help required on right query.


Solution

  • Try this:

    ALTER TABLE "user" ALTER COLUMN distinguishedName1 TYPE text USING code::text;
    

    or

    ALTER TABLE "user" ALTER COLUMN distinguishedName1 TYPE text
    

    Also do note that the USING is optional. See the manual here:

    The optional USING clause specifies how to compute the new column value from the old; if omitted, the default conversion is the same as an assignment cast from old data type to new. A USING clause must be provided if there is no implicit or assignment cast from old to new type.

    On a side note try to avoid naming your tables as reserved keywords.