4

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 ?

2 Answers 2

8

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.

1
  • 2
    The OP is using geography, which is implicitly 4326. While not required, using SRIDs on geometry is very much best practice.
    – Vince
    Mar 20, 2020 at 13:25
7

With ST_SetSRID it is possible to define explicitly what the coordinates like (-122.079513,45.607703) mean: for example that they are WGS84 long-lat degrees https://epsg.io/4326 and not something else in some other system like https://epsg.io/3857.

As documented for example in https://postgis.net/docs/ST_GeogFromText.html EPSG:4326 is the default for functions which deal with geography type.

Description

Returns a geography object from the well-known text or extended well-known representation. SRID 4326 is assumed if unspecified. This is an alias for ST_GeographyFromText. Points are always expressed in long lat form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.