H3-pg (developed by the open-source H3 / PostgreSQL geospatial community) is a native PostgreSQL extension that brings Uber's hierarchical hexagonal spatial indexing system (`H3`) directly into SQL queries and database tables. Used across ride-hailing, mobility analytics, logistics, delivery routing, telecom coverage, urban planning, and large-scale geospatial data warehousing, `h3-pg` lets data engineers convert coordinates into compact 64-bit `h3index` cell IDs, aggregate point/event data into uniform hexagonal grids, query neighboring rings (`k-ring` / `gridDisk`), and join massive spatial tables using standard B-tree indexes alongside `PostGIS`.
1. In-Depth Review: Why Geospatial & Data Engineers Use h3-pg in PostgreSQL
- Fast B-Tree & Group-By Aggregations Over Millions of Points: Indexing `h3index` (`BIGINT` / `h3index`) cells turns slow point-in-polygon or distance scans into fast hierarchical hex-bin aggregations (`GROUP BY h3_lat_lng_to_cell(...)`).
- Multi-Resolution Hierarchy (`Resolutions 0–15`) for Analytics & Visualization: Lets analysts roll up fine-grained hexagonal cells into coarser parent cells (`h3_cell_to_parent`) for regional dashboards or drill down into child cells (`h3_cell_to_children`) for local operations.
- Works Alongside `PostGIS` for Polygon Conversion & Map Rendering: Bridges discrete hexagonal cell IDs with `PostGIS` geometry/geography polygons (`h3_cell_to_boundary_geometry`) so hex grids can be exported to `QGIS`, `MapLibre GL JS`, or `deck.gl`.
2. Key Features & Capabilities
- Coordinate-to-Hex & Hex-to-Coordinate / Boundary Conversion: Convert WGS84 latitude/longitude or `PostGIS` points into `h3index` cells and generate hexagonal cell centers or `PostGIS` polygon boundaries.
- Hierarchical Parent, Child & Compaction Functions: Navigate parent/child cell relationships across 16 resolution levels and compact sets of child hexagons into minimal parent coverage.
- Neighbor & Grid Traversal (`gridDisk`, `gridRing`, `gridPathCells`, `gridDistance`): Find surrounding hex rings, compute grid distances between cells, and trace grid paths for spatial proximity and movement modeling.
- Polygon-to-Hex Coverage (`polygon_to_cells`): Fill administrative boundaries, service zones, or trade areas with H3 hexagonal cells at any target resolution.
- Fast B-Tree Indexing & SQL Analytics Integration: Index H3 cell columns directly in PostgreSQL and combine with `PostGIS`, `Citus`, `TimescaleDB`, or `DuckDB` analytical pipelines.
- 100% Free & Open-Source (`Apache 2.0` License): Free to compile, install, and run on self-hosted PostgreSQL servers, Docker containers, and compatible cloud PostgreSQL environments.
- Official Links: Source Repository, Documentation & H3 Standard
Always download source releases and verify function signatures directly from official `h3-pg` and H3 project portals:
- Official h3-pg GitHub Repository & Releases: https://github.com/PGZXB/h3-pg
- Official H3 Hierarchical Spatial Index Documentation: https://h3geo.org/docs/
- Official PostgreSQL Extensions & Contrib Documentation: https://www.postgresql.org/docs/current/contrib.html
4. Step-by-Step Installation Guide
- Check PostgreSQL Version & Build/Extension Support: Confirm your server runs a supported 64-bit `PostgreSQL` release (`13`–`17+`) on `Linux` (Ubuntu/Debian/RHEL), `macOS`, `Windows`, or `Docker`, with `postgresql-server-dev` and C compiler/CMake tools if building from source (or use a pre-built container/package).
- Download `h3-pg` Source or Extension Package: Visit https://github.com/PGZXB/h3-pg (or check your OS/container package repository) and fetch the `h3-pg` release matching your installed H3 (`v4.x`) and PostgreSQL versions.
- Build & Install the Extension on the PostgreSQL Host: Build and install using `make` / `cmake` and `make install` (with `PG_CONFIG` pointing to your target PostgreSQL installation) so the extension files are placed in PostgreSQL's `extension` and `lib` directories.
- Enable `CREATE EXTENSION h3;` in Your Database: Connect to your target database via `psql` or `pgAdmin` and run `CREATE EXTENSION IF NOT EXISTS h3;` (and `CREATE EXTENSION IF NOT EXISTS h3_postgis;` if using `PostGIS` geometry conversions).
<Image src="image_agent_tag_9347353875665221107" alt="pgAdmin SQL Query Editor window executing a SQL query on PostgreSQL" caption="pgAdmin SQL Query Editor on PostgreSQL" />
5. How to Use h3-pg: Beginner Workflow
- Enable the `h3` Extension: Open your SQL client (`psql` or `pgAdmin`) connected to your PostgreSQL database and run `CREATE EXTENSION IF NOT EXISTS h3;`.
- Convert Latitude/Longitude Points to H3 Cells: Compute an H3 cell index at resolution `8` or `9` using `SELECT h3_lat_lng_to_cell(POINT(104.75, 16.56), 8);` (note longitude/latitude order in PostgreSQL `POINT(lng, lat)`).
- Store & Index `h3index` in an Analytics Table: Add an `h3index` column to your events or trips table and create a B-tree index (`CREATE INDEX idx_events_h3 ON events(cell_res8);`) for fast spatial grouping.
- Aggregate Events by Hexagon & Neighbor Rings: Group records by cell (`SELECT cell_res8, COUNT(*) AS total_events FROM events GROUP BY cell_res8;`) or expand around a cell using `h3_grid_disk(cell, 1)`.
- Convert Hexagons to Geometry for Map Visualization: Use `h3_postgis` (`h3_cell_to_boundary_geometry(cell)`) when `PostGIS` is installed to render hexagonal heatmaps in `QGIS`, `MapLibre GL JS`, or `deck.gl`.
6. Frequently Asked Questions (FAQ)
- How does `h3-pg` differ from `PostGIS`?`PostGIS` stores exact vector geometries (`POINT`, `LINESTRING`, `POLYGON`, `RASTER`) and performs precise geometric predicates (`ST_Intersects`, `ST_Buffer`), whereas `h3-pg` indexes space into uniform hierarchical hexagonal cells (`h3index`) for ultra-fast grid aggregations, neighbor ring queries, and spatial binning.
- Do I need `PostGIS` installed to use `h3-pg`?No. Core `h3-pg` (`CREATE EXTENSION h3;`) works independently using PostgreSQL's built-in `POINT` type and `h3index`, while `h3_postgis` is optional when converting between H3 cells and `PostGIS` `geometry` types.
- Does `h3-pg` use `POINT(longitude, latitude)` ordering?Yes. In `h3-pg` functions accepting a PostgreSQL `POINT`, the X coordinate is longitude and the Y coordinate is latitude (`POINT(lng, lat)`).