Using PostgreSQL, what's the command to migrate an integer
column type to a string
column type?
Obviously I'd like to preserve the data, by converting the old integer data to strings.
Using PostgreSQL, what's the command to migrate an integer
column type to a string
column type?
Obviously I'd like to preserve the data, by converting the old integer data to strings.
You can convert from INTEGER
to CHARACTER VARYING
out-of-the-box, all you need is ALTER TABLE
query chaning column type:
PostgreSQL 9.3 Schema Setup:
CREATE TABLE tbl (col INT);
INSERT INTO tbl VALUES (1), (10), (100);
ALTER TABLE tbl ALTER COLUMN col TYPE CHARACTER VARYING(10);
Query 1:
SELECT col, pg_typeof(col) FROM tbl
| col | pg_typeof |
|-----|-------------------|
| 1 | character varying |
| 10 | character varying |
| 100 | character varying |
I suggest a four step process:
update myTable set temp=cast(intColumn as text)
see http://www.postgresql.org/docs/9.3/static/functions-formatting.html for more interesting number->string conversionsMake sure everything in temp looks the way you want it.
This assumes you can perform the operation while no clients are connected; offline. If you need to make this (drastic) change in an online table, take a look at setting up a new table with triggers for live updates, then swap to the new table in an atomic operation. see ALTER TABLE without locking the table?