Changing SRID of existing data in PostGIS?
There is a single line function which does this for you. Just use the following SQL query:
select UpdateGeometrySRID('Schema Name', 'mytable', 'the_geom', newSRID) ;
But, if you are like me, you would be interested in the low level, miniature steps. Logically speaking, the above function is equivalent to the following four step process:
In the geometry_columns table, update the SRID to the required value.
Drop the contraint on the table, by using the following SQL statement
ALTER TABLE mytable DROP CONSTRAINT enforce_srid_the_geom;
Update the SRID'd of the geometry by using the following SQL statement
UPDATE mytable SET the_geom = ST_SetSRID(the_geom, newSRID);
Add the contraint back by using the following SQL statement
ALTER TABLE mytable
ADD CONSTRAINT enforce_srid_the_geom CHECK (st_srid(the_geom) = (newSRID));
With PostGIS 2.x, geometry columns typically use typmods like geometry(Point, 1234)
. For these you can use ALTER TABLE to directly modify the geometry column type in one step.
For example, to set the SRID of geom
in mytable
to WGS84, use ST_SetSRID:
ALTER TABLE mytable
ALTER COLUMN geom
TYPE geometry(Point, 4326)
USING ST_SetSRID(geom, 4326);
Note that this will only alter the SRID, but not transform the coordinate data.