PostGIS – What Exactly Does ST_SetSRID Do in PostGIS

postgispostgresqlsrid

I am new to DBA and PostGIS. I am developing an application using PostgreSQL and PostGIS where nearby locations need to be queried.

I created a sample table as below:

CREATE TABLE foo (geog geography);

Inserting like:

INSERT INTO foo (geog)
  VALUES (ST_MakePoint(12.234,23.22));

But in some tutorials,insertion is like:

INSERT INTO foo (geog) VALUES (ST_SetSRID(ST_MakePoint(-122.079513,45.607703), 4326));

I can't clearly understand what ST_SetSRID does.
What does it do?
Do I need to use it If I am going to find nearby places, etc?
If I should use it, what value should I go with in the place of 4326 ?

Best Answer

ST_SetSRID will set the coordinate reference system of your geometry. This will allow PostGIS commands to understand how your grid will relate to other geometries.

Using ST_SetSRID is not essential, as long as all other geometries you may query against are known to be on the same grid. However, if you query against another table that has a SRID set, even if it is known to be of the same grid, PostGIS will fail with a 'mixed geometries' error, which is only resolved when both tables have the same SRID value.