3

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.

2
  • 3
    Where in the manual did you see the modify keyword? Commented Mar 28, 2016 at 12:39
  • 1
    "modify the data type to (...) based on user's input" that sounds like a horrible idea. What problem is it you are trying to solve with that? Commented Mar 28, 2016 at 12:40

3 Answers 3

3

POSTGRES syntax for altering column type :

ALTER TABLE user ALTER COLUMN distinguishedName1 TYPE text;
Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

I don't think USING is required here
The query "alter table user alter column distinguishedName1 type text using distinguishedName1::text; works fine for me. But the same query is not working if I try to change data type to integer or boolean.
1
alter table user Alter column distinguishedName1 text;

Syntax mistake , for sql server you have to use alter to modify the column of table

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.