Encodes a geometry object into a GeoHash string. A GeoHash encodes a geographic point into a compact text form that is sortable and searchable by prefix — a shorter GeoHash covers a larger area with lower precision, while a longer one covers a smaller area with higher precision.
Syntax
text ST_GeoHash(geometry geom [, integer maxchars])Parameters
| Parameter | Required | Description |
|---|---|---|
geom | Yes | The geometry object to encode. |
maxchars | No | The number of characters in the returned GeoHash string, which controls precision. If omitted, the function returns a GeoHash string based on the full precision of the geometry object. |
Usage notes
For non-point geometry objects, the function computes the GeoHash from the center of the bounding box.
Input must use a longitude- and latitude-based geographic coordinate system. Other coordinate systems are not supported.
Supports circular strings and curves.
Examples
Encode a point with default precision
SELECT ST_GeoHash(ST_GeomFromText('POINT(116 40)', 4326));Output:
st_geohash
----------------------
wx47x9u8gumnhzp791zb
(1 row)Encode a point with custom precision
Pass an integer as maxchars to limit the number of characters returned.
SELECT ST_GeoHash(ST_GeomFromText('POINT(116 40)', 4326), 3);Output:
st_geohash
------------
wx4
(1 row)A 3-character GeoHash covers a larger geographic area. Use a shorter GeoHash when you need to group nearby locations — for example, to index or bucket points by proximity.