postgresqlmacosinstallationpostgis

Enable Postgis extension for PostgreSQL 16 on Mac M2


I'm trying to install PostgreSQL and Postgis extension on a Mac with M2 processor.

I've tried to follow various online instructions and had no luck so far.

Below are my installation steps:

  1. I've installed PostgreSQL 16 using the Installer from https://www.postgresql.org/download/macosx/. The installation path for the PostgreSQL is at: /Library/PostgreSQL/16/

  2. Then I use 'brew install postgis' to install the postgis extension. It's installed at: /opt/homebrew/Cellar/postgis/3.3.4_1.

  3. Execute 'CREATE EXTENSION postgis;' in PostgreSQL and receive: Detail: Could not open extension control file "/Library/PostgreSQL/16/share/postgresql/extension/postgis.control": No such file or directory.

It seems that the postgis is installed into the brew location that PostgreSQL won't be able to locate. Wondering if anyone can tell me if i miss any obvious steps in the installation.

I also tried:

  1. add "shared_preload_libraries = 'postgis'" into PostgreSQL config file. But since PostgreSQL can't locate the postgis in /Library/PostgreSQL/16/share/postgresql/extension/. Server won't start after the config is changed.

  2. Some online link suggested to install postgis via Stack Builder. That option is not available in the latest version.

  3. It seems that I should get postgis 3.4.0 installed for PostgreSQL 16. Brew seems to install 3.3.4 by default for me for some reason. Wondering if this could be the cause of my issue.


Solution

  • Currently, the brew postgis formula is linked to PostgreSQL 14, but you can manually compile PostGIS for PostgreSQL 16 on a Mac M2.

    brew install postgresql@16
    
    brew install postgis
    # or install required libs/packages manually (brew install geos gdal libxml2 sfcgal ...)
    
    sudo ln -s /opt/homebrew/Cellar/postgresql@16/16.0/bin/postgres /usr/local/bin/postgres
    
    wget https://download.osgeo.org/postgis/source/postgis-3.4.0.tar.gz
    tar -xvzf postgis-3.4.0.tar.gz
    rm postgis-3.4.0.tar.gz
    cd postgis-3.4.0
    
    ./configure --with-projdir=/opt/homebrew/opt/proj --with-protobufdir=/opt/homebrew/opt/protobuf-c --with-pgconfig=/opt/homebrew/opt/postgresql@16/bin/pg_config --with-jsondir=/opt/homebrew/opt/json-c --with-sfcgal=/opt/homebrew/opt/sfcgal/bin/sfcgal-config --with-pcredir=/opt/homebrew/opt/pcre "LDFLAGS=$LDFLAGS -L/opt/homebrew/Cellar/gettext/0.22.2/lib" "CFLAGS=-I/opt/homebrew/Cellar/gettext/0.22.2/include"
    
    make
    make install