These integration guides are not official documentation and the Strapi Support Team will not provide assistance with them.
Why Use PostGIS
PostGIS beyond basic SQL capabilities by supporting geographic objects natively. For those interested in understanding geolocation data, this means you can run complex spatial queries directly in your database. Functions like spatial joins and data transformations become straightforward, and its GiST-based R-Tree indexes speed up your queries. Integrating PostGIS with Strapi allows you to leverage these powerful geospatial features within your content management system.
Given the benefits of PostGIS, when choosing the right database for Strapi, PostgreSQL with PostGIS becomes a compelling option. PostGIS also integrates seamlessly with popular GIS software, making it a natural choice for analysts and developers who rely on maps and location-based features. The open-source community keeps PostGIS in top shape, and the official PostGIS documentation offers detailed insights into everything it can do.
Why Use Strapi?
Strapi is the leading open-source headless CMS offering features, like customizable APIs, role-based permissions, multilingual support, etc. It simplifies content management and integrates effortlessly with modern frontend frameworks.
Explore the Strapi documentation for more details.
Strapi 5 Highlights
The out-of-the-box Strapi features allow you to get up and running in no time: 1. Single types: Create one-off pages that have a unique content structure. 2. Draft and Publish: Reduce the risk of publishing errors and streamline collaboration. 3. 100% TypeScript Support: Enjoy type safety & easy maintainability 4. Customizable API: With Strapi, you can just hop in your code editor and edit the code to fit your API to your needs. 5. Integrations: Strapi supports integrations with Cloudinary, SendGrid, Algolia, and others. 6. Editor interface: The editor allows you to pull in dynamic blocks of content. 7. Authentication: Secure and authorize access to your API with JWT or providers. 8. RBAC: Help maximize operational efficiency, reduce dev team support work, and safeguard against unauthorized access or configuration modifications. 9. i18n: Manage content in multiple languages. Easily query the different locales through the API. 10. Plugins: Customize and extend Strapi using plugins.
Learn more about Strapi 5 feature.
See Strapi in action with an interactive demo
Setup Strapi 5 Headless CMS
We are going to start by setting up our Strapi 5 project with the following command:
🖐️ Note: make sure that you have created a new directory for your project.
You can find the full documentation for Strapi 5 here.
Install Strapi
npx create-strapi-app@latest server
You will be asked to choose if you would like to use Strapi Cloud we will choose to skip for now.
Strapi v5.6.0 🚀 Let's create your new project
We can't find any auth credentials in your Strapi config.
Create a free account on Strapi Cloud and benefit from:
- ✦ Blazing-fast ✦ deployment for your projects
- ✦ Exclusive ✦ access to resources to make your project successful
- An ✦ Awesome ✦ community and full enjoyment of Strapi's ecosystem
Start your 14-day free trial now!
? Please log in or sign up.
Login/Sign up
❯ Skip
After that, you will be asked how you would like to set up your project. We will choose the following options:
? Do you want to use the default database (sqlite) ? Yes
? Start with an example structure & data? Yes <-- make sure you say yes
? Start with Typescript? Yes
? Install dependencies with npm? Yes
? Initialize a git repository? Yes
Once everything is set up and all the dependencies are installed, you can start your Strapi server with the following command:
cd server
npm run develop
You will be greeted with the Admin Create Account screen.
Go ahead and create your first Strapi user. All of this is local so you can use whatever you want.
Once you have created your user, you will be redirected to the Strapi Dashboard screen.
Publish Article Entries
Since we created our app with the example data, you should be able to navigate to your Article collection and see the data that was created for us.
Now, let's make sure that all of the data is published. If not, you can select all items via the checkbox and then click the Publish button.
Enable API Access
Once all your articles are published, we will expose our Strapi API for the Articles Collection. This can be done in Settings -> Users & Permissions plugin -> Roles -> Public -> Article.
You should have find
and findOne
selected. If not, go ahead and select them.
Test API
Now, if we make a GET
request to http://localhost:1337/api/articles
, we should see the following data for our articles.
🖐️ Note: The article covers (images) are not returned. This is because the REST API by default does not populate any relations, media fields, components, or dynamic zones.. Learn more about REST API: Population & Field Selection.
So, let's get the article covers by using the populate=*
parameter: http://localhost:1337/api/articles?populate=*
Getting Started with PostGIS
Before you dive into integrating PostGIS with Strapi, make sure PostgreSQL is installed and ready to go.
Installing PostGIS and Adjusting Settings in Strapi
Once Strapi is up and running, it's time to download and configure PostgreSQL from its official download page. You'll then install PostGIS on top of PostgreSQL using your package manager. On Ubuntu, for example:
sudo apt install postgis postgresql-<your-postgres-version>-postgis-<postgis-version>
Enable the PostGIS extension in your database:
1CREATE DATABASE mydatabase;
2\c mydatabase
3CREATE EXTENSION postgis;
With that, your database is prepped for spatial data. Next, navigate to Strapi's config/database.js
file and configure the PostgreSQL connection details as needed:
1module.exports = ({ env }) => ({
2 defaultConnection: 'default',
3 connections: {
4 default: {
5 connector: 'bookshelf',
6 settings: {
7 client: 'postgres',
8 host: env('DATABASE_HOST', 'localhost'),
9 port: env.int('DATABASE_PORT', 5432),
10 database: env('DATABASE_NAME', 'mydatabase'),
11 username: env('DATABASE_USERNAME', 'myusername'),
12 password: env('DATABASE_PASSWORD', 'mypassword'),
13 ssl: env.bool('DATABASE_SSL', false),
14 },
15 options: {},
16 },
17 },
18});
Replace the placeholders with your own credentials, then restart Strapi. For more information on configuring Strapi databases, you can refer to the official guide.
Implementing Geospatial Features in Strapi with PostGIS
You can add standard field types like text, numbers, media, and relational fields to your Strapi content types using the Strapi Content Types Builder. For spatial data, consider integrating external libraries or plugins that handle geospatial information.
To run advanced geospatial queries with PostgreSQL, you can tap into PostGIS functions like ST_Distance
, ST_Area
, or ST_Intersects
. These functions let you filter and analyze data based on distances, boundaries, or shape intersections. It's a good idea to test your setup with a tool like Postman to make sure requests involving geospatial data behave as you expect. If you're leveraging both REST and GraphQL APIs in your project, consider combining REST and GraphQL to optimize your data fetching strategies. Choosing the right API for geospatial data can impact performance; for insights, check out this REST vs GraphQL comparison.
If your project requires regular updates or processing of spatial data, you can automate tasks in Strapi using cron jobs, ensuring your geospatial information stays current.
If you want to dive deeper into what PostGIS can do, check out the PostGIS Manual. By integrating PostGIS with Strapi, you're set to build and maintain applications where location-based data is front and center.
Before wrapping up, double-check your database logs and API responses. A working PostGIS-enabled Strapi system gives you a powerful foundation for any project that needs spatial insights.
Awesome, great job!
Strapi Open Office Hours
If you have any questions about Strapi 5 or just would like to stop by and say hi, you can join us at Strapi's Discord Open Office Hours Monday through Friday at 12:30 pm - 1:30 pm CST: Strapi Discord Open Office Hours
For more details, visit the Strapi documentation and PostGIS documentation.