design-postgres-tables
Use this skill for general PostgreSQL table design. **Trigger when user asks to:** - Design PostgreSQL tables, schemas, or data models when creating new tables…
Comprehensive PostGIS spatial table design reference covering geometry types, coordinate systems, spatial indexing, and performance patterns for location-based applications
$ npx -y skills add timescale/pg-aiguide --skill design-postgis-tables --agent claude-codeHow it fires
How this skill gets triggered: by you, by Claude, or both.
/design-postgis-tablesContext preview
The summary Claude sees to decide when to auto-load this skill.
Comprehensive PostGIS spatial table design reference covering geometry types, coordinate systems, spatial indexing, and performance patterns for location-based applications
name: design-postgis-tables description: Comprehensive PostGIS spatial table design reference covering geometry types, coordinate systems, spatial indexing, and performance patterns for location-based applications license: Apache-2.0 compatibility: Requires PostgreSQL 15+ with the PostGIS extension metadata: author: tigerdata
1. What is the geographic scope (single city/region vs global)? 2. What are your primary query patterns (within-radius, bbox, intersects, nearest-neighbor)? 3. What units do you need for distance/area (meters vs CRS units), and how accurate must they be? 4. What is the expected scale (rows, write rate), and is the data mostly append-only? 5. 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.
-- Regional data with projected coordinates (UTM Zone 10N for California)
CREATE TABLE local_parcels (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
parcel_number TEXT NOT NULL,
boundary GEOMETRY(POLYGON, 26910), -- UTM Zone 10N (meters)
area_sqm DOUBLE PRECISION GENERATED ALWAYS AS (ST_Area(boundary)) STORED
);-- Global data with geodetic calculations
CREATE TABLE global_offices (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name TEXT NOT NULL,
city TEXT NOT NULL,
location GEOGRAPHY(POINT, 4326) -- WGS84 (lat/lon)
);
-- Distance in meters (accurate spherical calculation)
SELECT
a.name AS office_a,
b.name AS office_b,
ST_Distance(a.location, b.location) / 1000 AS distance_km
FROM global_offices a
CROSS JOIN global_offices b
WHERE a.id < b.id;| 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 |
-- Single location (stores, sensors, events) location GEOMETRY(POINT, 4326) -- Multiple discrete locations (multi-branch business) locations GEOMETRY(MULTIPOINT, 4326) -- 3D point with elevation location_3d GEOMETRY(POINTZ, 4326) -- Point with measure value (linear referencing) location_m GEOMETRY(POINTM, 4326)
**Use POINT for:** Store locations, sensor positions, event coordinates, addresses, POIs **Use MULTIPOINT for:** Multiple related locations stored as single feature
-- Single path (road segment, river, route) path GEOMETRY(LINESTRING, 4326) -- Multiple paths (road network, transit lines) network GEOMETRY(MULTILINESTRING, 4326) -- 3D line with elevation profile trail_3d GEOMETRY(LINESTRINGZ, 4326)
**Use LINESTRING for:** Roads, rivers, pipelines, GPS tracks, routes **Use MULTILINESTRING for:** Disconnected road segments, river systems
-- Single area (parcel, building footprint, zone) boundary GEOMETRY(POLYGON, 4326) -- Multiple areas (archipelago, fragmented habitat) territories GEOMETRY(MULTIPOLYGON, 4326) -- 3D polygon (building with height) footprint_3d GEOMETRY(POLYGONZ, 4326)
**Use POLYGON for:** Property boundaries, administrative areas, service zones **Use MULTIPOLYGON for:** Countries with islands, fragmented regions
-- Any geometry type (flexible schema) geom GEOMETRY(GEOMETRY, 4326) -- Collection of mixed types features GEOMETRY(GEOMETRYCOLLECTI
AI-optimized PostgreSQL expertise for coding assistants pg-aiguide helps AI coding tools write dramatically better PostgreSQL code.
Repo: timescale/pg-aiguide
Use this skill for general PostgreSQL table design. **Trigger when user asks to:** - Design PostgreSQL tables, schemas, or data models when creating new tables…
Use this skill to analyze an existing PostgreSQL database and identify which tables should be converted to Timescale/TimescaleDB hypertables. **Trigger when…
Use this skill to migrate identified PostgreSQL tables to Timescale/TimescaleDB hypertables with optimal configuration and validation. **Trigger when user asks…
Use this skill for setting up vector similarity search with pgvector for AI/ML embeddings, RAG applications, or semantic search. **Trigger when user asks to:**…
Use this skill for planning, testing, and safely executing PostgreSQL schema migrations — especially when working with production data or shared databases.…
Use this skill to implement hybrid search combining BM25 keyword search with semantic vector search using Reciprocal Rank Fusion (RRF). **Trigger when user…