timescale/pg-aiguide/design-postgis-tables-2
Claude Code and agent skill by timescale/pg-aiguide
- community
Checking that the SKILL.md file is still in the repository.
Comprehensive PostGIS spatial table design reference covering geometry types, coordinate systems, spatial indexing, and performance patterns for location-based applications
Install
npx vskill@latest install timescale/pg-aiguide/design-postgis-tables-2SKILL.md
PostGIS Spatial Table Design
Before You Start (5 Questions)
- What is the geographic scope (single city/region vs global)?
- What are your primary query patterns (within-radius, bbox, intersects, nearest-neighbor)?
- What units do you need for distance/area (meters vs CRS units), and how accurate must they be?
- What is the expected scale (rows, write rate), and is the data mostly append-only?
- Do you need 3D (Z) or measures (M), or is 2D enough?
SQL injection note: When turning these patterns into application code, use parameterized queries for user-provided values (WKT/WKB, coordinates, IDs, radii). Avoid string-concatenating untrusted input into SQL; for dynamic identifiers, use safe identifier quoting/whitelisting.
Core Rules
- Always use PostGIS geometry/geography types instead of PostgreSQL's built-in geometric types (
POINT,LINE,POLYGON,CIRCLE). PostGIS types provide true spatial capabilities. - Choose between GEOMETRY and GEOGRAPHY based on your use case: GEOMETRY for projected/local data with Cartesian math; GEOGRAPHY for global data requiring accurate spherical calculations.
- Always specify SRID (Spatial Reference Identifier) when creating geometry columns. Use
4326(WGS84) for GPS/global data, appropriate local projections for regional data. - Create spatial indexes on all geometry/geography columns using GiST (default). Consider BRIN only for very large GEOMETRY tables where rows are naturally ordered on disk and you can tolerate coarser filtering.
- Use constraint-based type enforcement with
GEOMETRY(type, SRID)syntax to ensure data integrity.
Geometry vs Geography
When to Use GEOMETRY
- Local/regional data within a single coordinate system
- Projected coordinates (meters, feet) for accurate area/distance calculations
- Complex spatial operations (buffering, unions, intersections)
- Performance-critical queries (Cartesian math is faster)
- Data already in a projected CRS (UTM, State Plane, etc.)
When to Use GEOGRAPHY
- Global data spanning multiple continents/hemispheres
- GPS coordinates (latitude/longitude in decimal degrees)
- Accurate distance calculations on Earth's surface (great circle)
- Simple spatial operations (distance, containment)
- Data from GPS devices, geocoding services, or web maps
Comparison Table
| Aspect | GEOMETRY | GEOGRAPHY | |
|---|---|---|---|
| Coordinate system | Any SRID (projected or geodetic) | WGS84 (SRID 4326) only | |
| Distance units | CRS units (degrees, meters, feet) | Meters (always) | |
| Distance accuracy | Depends on projection | True spheroidal distance | |
| Area accuracy | Accurate in projected CRS | Accurate on sphere | |
| Function support | Full (300+ functions) | Limited (~40 functions) | |
| Performance | Faster (Cartesian math) | Slower (spherical math) | |
| Index type | GiST, BRIN, SP-GiST | GiST only | |
| Best for | Regional/local data, complex analysis | Global data, GPS tracking |
Geometry Types
Point Types
Use POINT for: Store locations, sensor positions, event coordinates, addresses, POIs Use MULTIPOINT for: Multiple related locations stored as single feature
Line Types
Use LINESTRING for: Roads, rivers, pipelines, GPS tracks, routes Use MULTILINESTRING for: Disconnected road segments, river systems
Polygon Types
Use POLYGON for: Property boundaries, administrative areas, service zones Use MULTIPOLYGON for: Countries with islands, fragmented regions
Generic Types
Use GEOMETRY for: Flexible schemas accepting multiple types Avoid GEOMETRYCOLLECTION: Prefer homogeneous types for better indexing
Coordinate Systems (SRID)
Common SRIDs
| SRID | Name | Use Case | Units | |
|---|---|---|---|---|
| 4326 | WGS84 | GPS, global data, web maps | Degrees | |
| 3857 | Web Mercator | Web map tiles (display only) | Meters | |
| 26910-26919 | UTM Zones (US) | Regional analysis | Meters | |
| 32601-32660 | UTM Zones (North) | Regional analysis | Meters | |
| 32701-32760 | UTM Zones (South) | Regional analysis | Meters |
SRID Best Practices
- Store in WGS84 (4326) for interoperability and GPS data
- Transform to projected CRS for accurate measurements
- Never mix SRIDs in spatial operations without explicit transformation
- Use appropriate local CRS for area/distance calculations requiring high precision
Spatial Indexing
GiST Index (Default)
Most versatile spatial index. Use for all geometry/geography columns.
Supports: All spatial operators (&&, @>, <@, ~=, <->) Best for: General-purpose spatial queries, mixed query patterns
BRIN Index
Block Range Index for very large, naturally ordered datasets.
Supports: Bounding box operators (&&, @>, <@) Best for: Append-only tables, time-series spatial data, very large datasets (>100M rows) Trade-off: Much smaller than GiST, but less precise filtering
SP-GiST Index
Space-partitioned GiST for point data with specific distributions.
Best for: Point-only data, quadtree-friendly distributions Not for: Complex geometries, mixed types
Index Selection Guide
| Scenario | Index Type | Reasoning | |
|---|---|---|---|
| General spatial queries | GiST | Most versatile, supports all operators | |
| Very large, append-only | BRIN | Tiny footprint, good for time-ordered data | |
| Point-only, uniform distribution | SP-GiST | Efficient for point lookups | |
| Geography columns | GiST | Only supported option | |
| Composite spatial + attribute | GiST + B-tree | Separate indexes or expression index |
Table Design Examples
Points of Interest (POI)
Property Parcels
GPS Tracking
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/design-postgis-tables/SKILL.md
Popularity
- Repository stars
- 1,640
- Repository forks
- 83
- Repository npm downloads, 7 days
- 26
- 7-day trend score
- 48
Stars and forks are for the whole GitHub repository, not this skill alone. Downloads are for the repository's npm package. Refreshed 23 Mar 2026, 8 months ago.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/timescale/pg-aiguide/design-postgis-tables-2)