-
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
Open
charlesarchibong
wants to merge
4
commits into
serverpod:main
Choose a base branch
from
charlesarchibong:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
be40ebe
docs: Add PostGIS geography fields documentation
charlesarchibong 6adde98
docs: Fix numbering conflict for upgrade-to-postgis guide
charlesarchibong 2348f36
Merge remote-tracking branch 'upstream/main'
charlesarchibong 345f8ab
docs: Address PostGIS review nits
charlesarchibong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| --- | ||
| description: Store geospatial data in Serverpod models using GeographyPoint, GeographyLineString, GeographyPolygon, and GeographyGeometryCollection backed by PostGIS. | ||
| --- | ||
|
|
||
| # 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. | ||
|
|
||
| ```yaml | ||
| class: Region | ||
| table: region | ||
| fields: | ||
| name: String | ||
| features: GeographyGeometryCollection | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Swiftaxe marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| --- | ||
| description: Add PostGIS to a Serverpod project to enable geography fields, with steps for Docker, managed PostgreSQL services, and pgvector. | ||
| --- | ||
|
|
||
| # Upgrading to PostGIS support | ||
|
Swiftaxe marked this conversation as resolved.
|
||
|
|
||
| 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. | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.