rent
PostgreSQL extensions

PostGIS

What it is

PostGIS turns PostgreSQL into a spatial database. It adds geometry and geography types, spatial indexes, coordinate-system transforms, and hundreds of functions for measuring and relating locations and shapes.

What it provides

  • Points, lines, polygons, rasters, and geographic coordinates
  • Distance, containment, intersection, nearest-neighbor, and transform operations
  • GiST and SP-GiST spatial indexes for large location datasets

Use it for store locators, delivery zones, geofencing, route analysis, property boundaries, map tiles, or "find everything within five kilometers" searches.

Use it with Rent

extension postgis {
  name = "postgis"
}

model Post {
  id       BigInt         @id
  title    String
  location postgis.Point? @db.Geometry(Point, 4326)
}

Add rent-ext-postgis to your dependencies, enable Rent's postgres and extensions features, then run rent generate and rent migrate dev --name add_locations. The generated field is Option<Point>.

use rent_ext_postgis::Point;

let office = Point::try_new(-87.6298, 41.8781, 4326)?;

let nearby = client
    .post()
    .order_by_location_distance(office)?
    .limit(25)
    .all()
    .await?;

The generated create/update setters accept .location(office); .clear_location() stores SQL NULL. Coordinates and SRIDs are validated before binding, and native EWKB preserves the point's SRID through reads, projections, eager loading, and transactions. The generated spatial field currently represents a two-dimensional geometry point; other PostGIS types remain available through explicit SQL expressions.

Units matter: geometry calculations use the coordinate system's units. EPSG:4326 coordinates are degrees, so this ordering does not return meters or a geodesic distance. For meter-based radius searches, choose an appropriate projected coordinate system or an explicit geography calculation. Do not mix SRIDs.

Run the application

cargo run -p rent --example extension_05_postgis

Set DATABASE_URL to a disposable PostgreSQL server with vector, PostGIS, and citext available. The example creates and cleans up its own database; the role needs database-creation and extension-installation privileges. It saves Chicago and New York posts, finds the nearest point, then clears the location with an optimistic version update. The schema and workflow live in crates/rent/examples/native_extension_fields/, and nextest executes the same application through the extension matrix.

On this page