63

While importing my shapefile data to PostGIS, I did not select the proper Projection.

How do I now change the SRID of the data, without transforming the Coordinates?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Devdatta Tengshe
  • 41,311
  • 35
  • 139
  • 263

3 Answers3

115

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:

  1. In the geometry_columns table, update the SRID to the required value.

  2. Drop the contraint on the table, by using the following SQL statement

    ALTER TABLE mytable DROP CONSTRAINT enforce_srid_the_geom;

  3. Update the SRID'd of the geometry by using the following SQL statement

    UPDATE mytable SET the_geom = ST_SetSRID(the_geom, newSRID);

  4. 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));

Devdatta Tengshe
  • 41,311
  • 35
  • 139
  • 263
  • 2
    See http://postgis.org/docs/ST_SetSRID.html for more info and links – BradHards Oct 01 '12 at 09:12
  • I only use this "UPDATE mytable SET the_geom = ST_SetSRID(the_geom, newSRID);" – Md Amiruzzaman Mar 31 '20 at 02:18
  • @MdAmiruzzamanThis question is from the 1.x version of PostGIS, where the geometries_columns table was used; Now with 2.x, with this table not being used, you don't need to go via such a long process, but if you have constraint on your table, then that needs to be updated as well – Devdatta Tengshe Mar 31 '20 at 10:45
  • 2
    UpdateGeometrySRID don't work if you have view on table. you need drop all views using your table before update geometry and them recreate all your views – GeoStoneMarten Aug 21 '20 at 07:46
  • https://postgis.net/docs/ST_SetSRID.html -- link has moved – jcollum Oct 24 '23 at 19:19
20

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.

Mike T
  • 42,095
  • 10
  • 126
  • 187
  • 2
    With a trivial change, this will also work with geography columns, which UpdateGeometrySRID does not support. – dsz Jan 31 '19 at 03:16
0
ALTER TABLE public.kltask ALTER COLUMN geom TYPE geometry(Point, 4326) USING ST_SetSRID(geom, 4326);
MrXsquared
  • 34,292
  • 21
  • 67
  • 117