PostGIS
Custom types, arrays and PostGIS are frequently presented together since geospatial data is often complex and multidimensional.
Overview
It’s therefore recommend to first quickly scan the content in Custom Types, Arrays and JSON
PowerSync integrates well with PostGIS and provides tools for working with geo data.
PostGIS
In Supabase, the PostGIS extension needs to be added to your project to use this type. Run the following command in the SQL editor to include the PostGIS extension:
The geography
and geometry
types are now available in your Postgres.
Supabase Configuration Example:
This example builds on the To-Do List example app in our Supabase integration guide.
Add custom type, array and PostGIS columns to the todos
table
Insert a row of data into the table
Note the following:
Custom type: Specify the value for the address
column by wrapping the value in single quotes and comma separate the different location_address properties.
'("1000 S Colorado Blvd.","Denver","CO",80211)'
Array: Specify the value of the contact_numbers
column, by surrounding the comma-separated array items in curly braces.
'{000-000-0000, 000-000-0000, 000-000-0000}'
PostGIS: Specify the value of the location
column by using the st_point
function and pass in the latitude and longitude
st_point(39.742043, -104.991531)
What this data looks like when querying from the PowerSync Dashboard
These data types show up as follows when querying from the PowerSync Dashboard’s SQL Query editor:
location |
---|
0101000020E6100000E59CD843FBDE4340E9818FC18AC052C0 |
This is Postgres’ internal binary representation of the PostGIS type.
On the Client
AppSchema example
Note:
- The custom type, array and PostGIS type have been defined as
TEXT
in the AppSchema. The Postgres PostGIS capabilities are not available because the PowerSync SDK uses SQLite, which only has a limited number of types. This means that everything is replicated into the SQLite database as TEXT values. - Depending on your application, you may need to implement functions in the client to parse the values and then other functions to write them back to the Postgres database.
What does the data look like in SQLite?
The data looks exactly how it’s stored in the Postgres database i.e.
- Custom Type: It has the same format as if you inserted it using a SQL statement, i.e.
(1000 S Colorado Blvd.,Denver,CO,80211)
- Array: Array types act similar in that it shows the data in the same way it was inserted e.g
{000-000-0000, 000-000-0000, 000-000-0000}
- PostGIS: The
geography
type is transformed into an encoded form of the value.- If you insert coordinates as
st_point(39.742043, -104.991531)
then it is shown as0101000020E6100000E59CD843FBDE4340E9818FC18AC052C0
- If you insert coordinates as
Sync Rules
PostGIS
Example use case: Extract x (long) and y (lat) values from a PostGIS type, to use these values independently in an application.
Currently, PowerSync supports the following functions that can be used when selecting data in your sync rules: Operators and Functions
ST_AsGeoJSON
ST_AsText
ST_X
ST_Y
IMPORTANT NOTE: These functions will only work if your Postgres instance has the PostGIS extension installed and you’re storing values as type geography
or geometry
.
Was this page helpful?