Postgis query patterns
Skill buildmoonshot/skillpacks/skills/gis/intermediate/postgis-query-patterns
A beginner-to-expert curriculum of drop-in skills for Claude Code, Codex, and any coding agent. Copy-paste ready, tested, not a link farm.
npx -y skills add buildmoonshot/skillpacks --skill postgis-query-patternsAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 2 stars2 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
What its author says it does
Copied from the file, not written here
Use when writing PostGIS or spatial SQL — distance, proximity, intersection, or geometry storage queries. Makes the agent use correct SRIDs, spatial indexes, and the right ST_ functions, so spatial SQL is both correct and fast instead of silently slow or wrong.
SKILL.md
1.8 KB, as published. Nobody here has run it
PostGIS Query Patterns
PostGIS rewards correct spatial SQL and quietly punishes the rest with wrong answers or full table scans. Follow the patterns.
Storage & SRID
- Store geometry with a known SRID (typmod like
geometry(Point, 4326)orST_SetSRID). Operations between different SRIDs error or mislead —ST_Transformto align. - Choose
geographyfor accurate distances over large/global areas (meters on a sphere);geometryin an appropriate projected CRS for fast planar math.
Make queries use the index
- Put a GiST index on every geometry column you query.
- Use index-assisted operators/functions:
&&,ST_Intersects,ST_DWithin. These hit the index. - Avoid
ST_Distance(a, b) < xin a WHERE clause — it can't use the index and forces a full scan. UseST_DWithin(a, b, x)instead.
Correctness gotchas
&&is bounding-box only — fast but approximate. Use it as a prefilter, thenST_Intersectsfor the exact test.- Validate geometry before overlays (
ST_IsValid/ST_MakeValid) — seevalidate-geometry. ST_DWithindistance units follow the type: meters forgeography, CRS units forgeometry(degrees if you left it in 4326 — usually not what you want for a distance).
Why this matters
Spatial SQL fails quietly in two directions: wrong SRID or wrong function gives a confident wrong answer, and a missing index turns a sub-second query into a minutes-long table scan that still "works" in testing and falls over in production. The patterns above keep queries both correct and fast.