Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
A

activerecord-postgis-adapter

> 后端框架
Open source

ActiveRecord connection adapter for PostGIS, based on postgresql and rgeo

936 stars0 likes0 views
WebsiteGitHub

About

ActiveRecord connection adapter for PostGIS, based on postgresql and rgeo

ActiveRecord PostGIS Adapter

The activerecord-postgis-adapter provides access to features of the PostGIS geospatial database from ActiveRecord. It extends the standard postgresql adapter to provide support for the spatial data types and features added by the PostGIS extension. It uses the RGeo library to represent spatial data in Ruby.

Overview

The adapter provides three basic capabilities:

First, it provides spatial migrations. It extends the ActiveRecord migration syntax to support creating spatially-typed columns and spatial indexes. You can control the various PostGIS-provided attributes such as SRID, dimension, and geographic vs geometric math.

Second, it recognizes spatial types and casts them properly to RGeo geometry objects. The adapter can configure these objects automatically based on the SRID and dimension in the database table, or you can tell it to convert the data to a different form. You can also set attribute data using WKT format.

Third, it lets you include simple spatial data in queries. WKT format data and RGeo objects can be embedded in where clauses.

Install

The adapter requires PostgreSQL 9.0+ and PostGIS 2.4+.

Installing PostGIS

Here are common methods for installing PostGIS, but more detailed methods can be found on the installation guide.

MacOS

bash
brew install postgis

Ubuntu/Debian

bash
sudo apt-get install postgis postgresql-16-postgis-3

Windows

PostGIS is likely available as an optional package via your Postgresql installer. If not, refer to the installation guide.

Version compatibility

Version ActiveRecord Ruby PostGIS Note
11.1.x 8.1 3.2.0+ 2.0+
11.0.x 8.0 3.2.0+ 2.0+
10.x 7.2 3.1.0+ 2.0+
9.x 7.1 3.0.0+ 2.0+
8.x 7.0 2.7.0+ 2.0+
7.x 6.1 2.5.0+, JRuby 2.0+
6.x 6.0 2.5.0+, JRuby 2.0+
5.x 5.1, 5.2 2.2.2+, JRuby 2.0+
4.x 5.0 2.2.2+, JRuby 2.0+
3.x 4.2 1.9.3+, JRuby 2.0+
2.x 4.0.0 - 4.1.x 1.9.3+, JRuby 2.0+ Read 2.x README
0.6.x 3.x 1.8.7+, JRuby, Rubinius 1.5+ Read 0.6.x / 2.x README
PostGIS 1 Notes

Upgrading to version 8.x

The PostgisDatabaseTasks module has been removed which means that the rake tasks to install postgis are no longer available. If using a Rails app, please see Upgrading an Existing Database

Upgrading from 6.x

When upgrading from version 6.x to a newer major version, you may need to modify your SpatialFactoryStore configuration. Please see this section of the README in rgeo-activerecord for more details (https://github.com/rgeo/rgeo-activerecord#spatial-factories-for-columns).

database.yml

You must modify your config/database.yml file to use the postgis adapter. At minimum, you will need to change the adapter field from postgresql to postgis. Recommended configuration:

yaml
development:
  username: your_username
  adapter: postgis
  host: localhost
  schema_search_path: public

If you have installed your PostGIS extension in a schema other than public, which is the default, add that schema to your schema_search_path:

yaml
development:
  schema_search_path: public, postgis

Here are some other options that are supported:

yaml
development:
  adapter: postgis
  encoding: unicode
  postgis_extension: postgis # default is postgis
  postgis_schema: public # default is public
  schema_search_path: public,postgis
  pool: 5
  database: my_app_development # your database name
  username: my_app_user # the username your app will use to connect
  password: my_app_password # the user's password
  su_username: my_global_user # a superuser for the database
  su_password: my_global_pasword # the superuser's password
rgeo dependency

This adapter uses the rgeo gem, which has additional dependencies. Please see the README documentation for rgeo for more information: https://github.com/rgeo/rgeo

Setup

If you have not created your rails app yet start there.

bash
rails new my_app --database=postgresql

Add the gem to your Gemfile.

ruby
gem 'activerecord-postgis-adapter'

And tell ActiveRecord to use the adapter by setting the adapter field in config/database.yml

yaml
default: &default
  adapter: postgis

Create the database if you haven't already.

bash
rake db:create

Create a migration to add the PostGIS extension to your database.

bash
rails generate migration AddPostgisExtensionToDatabase

The migration should look something like this:

ruby
class AddPostgisExtensionToDatabase  factory.point(-122, 47)).first

You can also use WKT:

ruby
record3 = MySpatialTable.where(:lonlat => 'POINT(-122 47)').first

Note that these queries use representational equality, meaning they return records where the lonlat value matches the given value exactly. A 0.00001 degree difference would not match, nor would a different representation of the same geometry (like a multipoint with a single element). Equality queries aren't generally all that useful in real world applications. Typically, if you want to perform a spatial query, you'll look for, say, all the points within a given area. For those queries, you'll need to use the standard spatial SQL functions provided by PostGIS.

To perform more advanced spatial queries, you can use the extended Arel interface included in the activerecord-postgis-adapter. The functions accept WKT strings or RGeo features.

rb
point = RGeo::Geos.factory(srid: 0).point(1,1)

buildings = Building.arel_table
containing_buiildings = Building.where(buildings[:geom].st_contains(point))

See rgeo-activerecord for more information about advanced spatial queries.

Joining Spatial Columns

If a spatial column is joined with another model, srid and geographic will not be automatically inferred and they will default to 0 and false, by default. In order to properly infer these options after a join, an attribute must be created on the target table.

ruby
class SpatialModel  RGeo::Geographic::SphericalPointImpl
p foo.geo_point.srid
# => 4326

Background: PostGIS

A spatial database is one that includes a set of data types, functions, tables, and other objects related to geospatial data. When these objects are present in your database, you can use them to store and query spatial objects such as points, lines, and polygons.

PostGIS is an extension for PostgreSQL that provides definitions for the objects you need to add to a database to enable geospatial capabilities.

When you create your Rails database as described above in the section on installation and configuration, activerecord-postgis-adapter automatically invokes PostGIS to add the appropriate definitions to your database. You can determine whether your database includes the correct definitions by attempting to invoke the POSTGIS_VERSION function:

sql
SELECT POSTGIS_VERSION(); # succeeds if PostGIS objects are present.

Standard spatial databases also include a table called spatial_ref_sys. This table includes a set of "spatial reference systems", or coordinate systems--- for example, WGS84 latitude and longitude, or Mercator Projection. Spatial databases also usually include a table called geometry_columns, which includes information on each database column that includes geometric data. In recent versions of PostGIS, geometry_columns is actually not a table but a view into the system catalogs.

Development and Support

RubyDoc Documentation is available at https://rubydoc.info/gems/activerecord-postgis-adapter

Contributions are welcome. See CONTRIBUTING.md for instructions.

Report issues at https://github.com/rgeo/activerecord-postgis-adapter/issues

Support is also available on the rgeo-users google group at https://groups.google.com/group/rgeo-users

Acknowledgments

Daniel Azuma authored the PostGIS Adapter and its supporting libraries (including RGeo).

Tee Parham is a former maintainer.

Keith Doggett is a current maintainer.

Ulysse Buonomo is a current maintainer.

Development is supported by:

  • Klaxit
  • Goldfish Ads

This adapter implementation owes some debt to the spatial_adapter plugin (https://github.com/fragility/spatial_adapter). Although we made some different design decisions for this adapter, studying the spatial_adapter source gave us a head start on the implementation.

License

Copyright Daniel Azuma, Tee Parham

https://github.com/rgeo/activerecord-postgis-adapter/blob/master/LICENSE.txt

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyactiverecordpostgispostgresqlrails

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架