I have the following line in a .sql file from a mysql db:
ALTER TABLE lcr_gw ALTER COLUMN ip_addr TYPE VARCHAR(50) DEFAULT NULL;
I would like to convert it into syntax that postgresql would understand. In my personal tests, I was only able to get it to work by breaking it down into two separate statements, like so:
ALTER TABLE lcr_gw ALTER COLUMN ip_addr TYPE VARCHAR(50);
ALTER TABLE lcr_gw ALTER COLUMN ip_addr SET DEFAULT NULL;
Just wondering if there's a way to consolidate the two statements back into one, but one that postgresql will be happy with?
Thanks!
The statement you posted is not valid syntax at all:
SQL Fiddle
To change the type in MySQL, you would use
CHANGE
orMODIFY
.To change the default you would use
DROP DEFAULT
orSET DEFAULT NULL
.If the intention was to change the type and reset the column default:
Like in MySQL, you can pack multiple actions into a single
ALTER TABLE
statement in Postgres .Per documentation:
But if there was a
DEFAULT
on the column that is incompatible with the new type, you have to run two separate statements:Doesn't matter in this case anyway.
As @Gordon Linoff states in the comments, postgreSQL by default sets a value to null unless a value is given or the default is changed to something else;
therefore, all you'll need is:
The PostgreSQL ALTER TABLE syntax diagram doesn't show any way to combine changing a data type and changing a default value in a single SQL statement. You can't simply omit
set default null
in the general case. For example,Instead, either rewrite as two independent statements (which you already know how to do), or as two statements in a single transaction.