-
Notifications
You must be signed in to change notification settings - Fork 89
docs: Add PostGIS geography fields documentation #502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,62 @@ | ||||||
| # Geography fields | ||||||
|
|
||||||
| Geography types are used for storing geospatial data on the surface of the Earth. They are stored as PostGIS geography columns in PostgreSQL using the WGS 84 coordinate system (SRID 4326), which is the standard used by GPS. | ||||||
|
|
||||||
| All geography types support spatial filter operations such as proximity search, intersection, containment, and distance-based ordering. See the [Geography operators](../database/filter#geography-operators) section for details. | ||||||
|
|
||||||
| To ensure optimal performance with spatial queries, consider creating a GIST index on your geography fields. See the [Geography indexes](../database/indexing#geography-indexes) section for more details. | ||||||
|
|
||||||
| :::info | ||||||
| The usage of Geography fields requires the PostGIS PostgreSQL extension to be installed. To set up PostGIS in a new or existing project, see the [Upgrading to PostGIS support](../../upgrading/upgrade-to-postgis) guide. | ||||||
| ::: | ||||||
|
|
||||||
| ## GeographyPoint | ||||||
|
|
||||||
| The `GeographyPoint` type stores a single geographic location defined by longitude and latitude. | ||||||
|
|
||||||
| ```yaml | ||||||
| class: Store | ||||||
| table: store | ||||||
| fields: | ||||||
| name: String | ||||||
| location: GeographyPoint | ||||||
| address: String? | ||||||
| ``` | ||||||
|
|
||||||
| ## GeographyLineString | ||||||
|
|
||||||
| The `GeographyLineString` type stores an ordered sequence of points forming a path or route. | ||||||
|
|
||||||
| ```yaml | ||||||
| class: DeliveryRoute | ||||||
| table: delivery_route | ||||||
| fields: | ||||||
| name: String | ||||||
| path: GeographyLineString | ||||||
| description: String? | ||||||
| ``` | ||||||
|
|
||||||
| ## GeographyPolygon | ||||||
|
|
||||||
| The `GeographyPolygon` type stores a closed region defined by an exterior ring and optional interior holes. | ||||||
|
|
||||||
| ```yaml | ||||||
| class: DeliveryZone | ||||||
| table: delivery_zone | ||||||
| fields: | ||||||
| name: String | ||||||
| boundary: GeographyPolygon | ||||||
| description: String? | ||||||
| ``` | ||||||
|
|
||||||
| ## GeographyGeometryCollection | ||||||
|
|
||||||
| The `GeographyGeometryCollection` type stores a collection of mixed geography types — points, lines, and polygons — as a single field. | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: we are avoiding em dashes.
Suggested change
|
||||||
|
|
||||||
| ```yaml | ||||||
| class: Region | ||||||
| table: region | ||||||
| fields: | ||||||
| name: String | ||||||
| features: GeographyGeometryCollection | ||||||
| ``` | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The upgrading folder was moved. Consider this when rebasing. |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,116 @@ | ||||||||||||||
| # Upgrading to PostGIS support | ||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| New Serverpod projects do not include PostGIS by default. To use geography fields in your models, you need a PostgreSQL instance with the PostGIS extension installed. | ||||||||||||||
|
|
||||||||||||||
| :::info | ||||||||||||||
| This upgrade is only necessary if you want to use geography fields in your models. If you do not plan to use geography fields, you can skip this upgrade. | ||||||||||||||
| ::: | ||||||||||||||
|
|
||||||||||||||
| :::warning | ||||||||||||||
| If trying to use geography fields without upgrading, you will encounter an error when applying migrations. | ||||||||||||||
| ::: | ||||||||||||||
|
|
||||||||||||||
| ## For Docker-based environments | ||||||||||||||
|
|
||||||||||||||
| 1. Update your `docker-compose.yml` to use a PostgreSQL image with PostGIS (e.g., `postgis/postgis:16-3.5`): | ||||||||||||||
|
|
||||||||||||||
| ```yaml | ||||||||||||||
| services: | ||||||||||||||
| postgres: | ||||||||||||||
| image: postgis/postgis:16-3.5 # <-- Change from postgres image here | ||||||||||||||
| ports: | ||||||||||||||
| - '8090:5432' | ||||||||||||||
| environment: | ||||||||||||||
| POSTGRES_USER: postgres | ||||||||||||||
| POSTGRES_DB: <projectname> | ||||||||||||||
| POSTGRES_PASSWORD: <DB_PASSWORD> | ||||||||||||||
| volumes: | ||||||||||||||
| - <projectname>_data:/var/lib/postgresql/data | ||||||||||||||
|
|
||||||||||||||
| # Other services... | ||||||||||||||
|
|
||||||||||||||
| postgres_test: | ||||||||||||||
| image: postgis/postgis:16-3.5 # <-- Change from postgres image here | ||||||||||||||
| ports: | ||||||||||||||
| - '9090:5432' | ||||||||||||||
| environment: | ||||||||||||||
| POSTGRES_USER: postgres | ||||||||||||||
| POSTGRES_DB: <projectname>_test | ||||||||||||||
| POSTGRES_PASSWORD: <DB_TEST_PASSWORD> | ||||||||||||||
| volumes: | ||||||||||||||
| - <projectname>_test_data:/var/lib/postgresql/data | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| If your project also uses [vector fields](../concepts/models/vector-fields), you need both pgvector and PostGIS. Create a custom `Dockerfile` instead: | ||||||||||||||
|
|
||||||||||||||
| ```dockerfile | ||||||||||||||
| FROM pgvector/pgvector:pg16 | ||||||||||||||
| RUN apt-get update \ | ||||||||||||||
| && apt-get install -y --no-install-recommends postgresql-16-postgis-3 \ | ||||||||||||||
| && rm -rf /var/lib/apt/lists/* | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| Then reference it in your `docker-compose.yml`: | ||||||||||||||
|
|
||||||||||||||
| ```yaml | ||||||||||||||
| services: | ||||||||||||||
| postgres: | ||||||||||||||
| build: | ||||||||||||||
| context: . | ||||||||||||||
| dockerfile: Dockerfile | ||||||||||||||
| ports: | ||||||||||||||
| - '8090:5432' | ||||||||||||||
| environment: | ||||||||||||||
| POSTGRES_USER: postgres | ||||||||||||||
| POSTGRES_DB: <projectname> | ||||||||||||||
| POSTGRES_PASSWORD: <DB_PASSWORD> | ||||||||||||||
| volumes: | ||||||||||||||
| - <projectname>_data:/var/lib/postgresql/data | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| <!-- markdownlint-disable-next-line MD029--> | ||||||||||||||
| 2. Recreate your containers to use the new image: | ||||||||||||||
|
|
||||||||||||||
| ```bash | ||||||||||||||
| docker compose down | ||||||||||||||
| docker compose up -d | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| <!-- markdownlint-disable-next-line MD029--> | ||||||||||||||
| 3. Create your first geography field in a model: | ||||||||||||||
|
|
||||||||||||||
| ```yaml | ||||||||||||||
| class: Store | ||||||||||||||
| table: store | ||||||||||||||
| fields: | ||||||||||||||
| name: String | ||||||||||||||
| location: GeographyPoint | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| <!-- markdownlint-disable-next-line MD029--> | ||||||||||||||
| 4. Generate and apply a migration: | ||||||||||||||
|
|
||||||||||||||
| ```bash | ||||||||||||||
| $ serverpod create-migration | ||||||||||||||
| $ dart run bin/main.dart --apply-migrations | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| For more details on creating and applying migrations, see the [Migrations](../concepts/database/migrations) section. | ||||||||||||||
|
|
||||||||||||||
| The PostGIS extension will be automatically enabled during the first migration that includes a geography column. | ||||||||||||||
|
|
||||||||||||||
| ## For managed PostgreSQL services | ||||||||||||||
|
|
||||||||||||||
| For cloud providers (AWS RDS, Google Cloud SQL, Azure Database, etc.), ensure that the PostGIS extension is available on your PostgreSQL instance. Most major managed services support PostGIS with no additional setup required. If available, the extension will be enabled automatically when applying the migration. | ||||||||||||||
|
|
||||||||||||||
| :::tip | ||||||||||||||
| If the cloud provider instructs you to run a `CREATE EXTENSION postgis;` command, you can skip this step as Serverpod will handle it automatically during migration. | ||||||||||||||
| ::: | ||||||||||||||
|
|
||||||||||||||
| ## Troubleshooting | ||||||||||||||
|
|
||||||||||||||
| If you encounter issues with PostGIS: | ||||||||||||||
|
|
||||||||||||||
| - Verify that your PostgreSQL version is 12 or later. | ||||||||||||||
| - Check that the PostGIS extension is properly installed on the instance. | ||||||||||||||
| - Ensure your database user has the necessary permissions to create extensions. | ||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a new convention, we are adding frontmatter descriptions to new pages for SEO.