Setting up PostGIS on Laravel Homestead

PostGIS is a geospatial database extension for PostgreSQL which appears to be useful for dealing with geospatial and geographical data natively within your database, such as having a bespoke point column type and being able to query (in SQL) for all of the records that have a point within a given polygon.

I wanted to use PostGIS in an Lumen microservice to handle overlaying points on a map, and being able to provide GeoJSON for each of the points at a set of coordinates within a given radius. Firstly I used Valet with for a minimum spec solution (my old MacBook Pro is getting on a bit now), which unfortunately brought along some inconsistencies with PHP versioning and .ini files I just didn’t want to get into. I then attempted to use Docker and Docker Compose to set up exactly the environment I was after but this proved overly complex (I’m still learning Docker). I finally realised that this could be achieved relatively easily using Laravel Homestead with minimal effort.

Homestead, when installed in to the ~/.homestead directory, provides a shell script,, which allows you to run any arbitrary provisioning code after the regular Homestead provisioning has been done. Firstly, I found the correct set of incantations (bash commands) needed to set up PostGIS, and added those to the command.

Firstly we need to update the repositories:

Next we need to install PostGIS and the compatibility pack for the version of PostgreSQL running on Homestead (as of writing this is v.9.4):

sudo apt-get install -y postgis postgresql-9.4-postgis-2.3

Then we need to enable the postgis extenstion for each of the databases you will be using, so you’ll need to add the following line for each PostGIS database (don’t forget to change the database name at the end there):

sudo -u postgres psql -c "CREATE EXTENSION postgis; CREATE EXTENSION postgis_topology;" YOUR_DATABASE_NAME_HERE

And that’s pretty much it. All you need to do then is add those lines to ~/.homestead/, provision the box with homestead provision and you should be good to go. If all goes well you should see the packages being added, and the extension being enabled with the CREATE EXTENSION output.

Now to actually figure out how to use it…

Alternatively we could not and say we did, and instead use MySQL’s native geospatial datatypes which have been included since version 5.6

Originally published at on October 9, 2016.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.