Install PostGIS for AlloyDB Omni

AlloyDB Omni does not include the PostGIS extension, but you can manually add it to an existing AlloyDB Omni installation by following the instructions on this page.

Before you begin

Ensure that you meet the following system requirements:

Add PostGIS to your AlloyDB Omni installation

To add the PostGIS extension to your AlloyDB Omni installation, follow these steps:

  1. Find your installed AlloyDB Omni version labels:
    sudo alloydb version

    The output is similar to the following:

    AlloyDB Omni CLI version: 1.0
    AlloyDB Omni database server version: 15.2.0
    

    Take note of the database server version number; you need it in the next step.

  2. Set the OMNI_VERSION environment variable:
    OMNI_VERSION=VERSION

    Replace VERSION with the complete database server version from the previous step—for example, 15.2.0.

  3. Create a new AlloyDB Omni container that includes PostGIS:
    mkdir ~/alloydb-omni-postgis
    
    tee -a ~/alloydb-omni-postgis/Dockerfile << EOF
    ARG OMNI_VERSION
    FROM postgres:15-bookworm as postgres
    
    RUN apt-get update && \
          apt-get install -y --no-install-recommends \
          postgresql-15-postgis-3 && \
          apt-get purge -y --auto-remove && \
          rm -rf /var/lib/apt/lists/*
    
    FROM gcr.io/alloydb-omni/pg-service:${OMNI_VERSION}
    
    COPY --from=postgres /usr/lib/postgresql/15/lib/postgis-3.so /lib/postgis-3.so
    COPY --from=postgres /usr/lib/postgresql/15/lib/postgis_sfcgal-3.so /lib/postgis_sfcgal-3.so
    COPY --from=postgres /usr/lib/postgresql/15/lib/postgis_raster-3.so /lib/postgis_raster-3.so
    COPY --from=postgres /usr/lib/postgresql/15/lib/postgis_topology-3.so /lib/postgis_topology-3.so
    COPY --from=postgres /usr/lib/postgresql/15/lib/address_standardizer-3.so /lib/address_standardizer-3.so
    COPY --from=postgres /usr/share/postgresql/15/extension/address_standardizer* /share/extension/
    COPY --from=postgres /usr/share/postgresql/15/extension/postgis* /share/extension/
    
    COPY --from=postgres /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/
    COPY --from=postgres /lib/x86_64-linux-gnu/ /lib/x86_64-linux-gnu/
    
    COPY --from=postgres /usr/lib/x86_64-linux-gnu/liblapack.so.3 /usr/lib/
    COPY --from=postgres /usr/lib/x86_64-linux-gnu/libblas.so.3 /usr/lib/
    COPY --from=postgres /usr/lib/libarmadillo.so.11 /usr/lib/
    COPY --from=postgres /usr/lib/libmfhdfalt.so.0 /usr/lib/
    COPY --from=postgres /usr/lib/libmfhdfalt.so.0 /usr/lib/
    COPY --from=postgres /usr/lib/libdfalt.so.0 /usr/lib/
    COPY --from=postgres /usr/lib/libogdi.so.4.1 /usr/lib/
    COPY --from=postgres /usr/share/proj/proj.db /usr/share/proj/proj.db
    
    EOF
    
    cd ~/alloydb-omni-postgis
    
    sudo docker build --build-arg OMNI_VERSION=$OMNI_VERSION --tag gcr.io/alloydb-omni/pg-service-with-postgis:$OMNI_VERSION .
  4. Stop the database server:
    sudo alloydb database-server stop
    
  5. Open /var/alloydb/config/dataplane.conf in a text editor.
  6. Modify the line that defines the PG_IMAGE variable so that it refers to the container that you built in a previous step:
    PG_IMAGE=gcr.io/alloydb-omni/pg-service-with-postgis
    
  7. Start AlloyDB Omni:
    sudo alloydb database-server start
    
  8. Connect to your database:
    sudo docker exec -it pg-service psql -h localhost -U postgres
    
  9. Enable PostGIS:
    CREATE EXTENSION IF NOT EXISTS POSTGIS;
    
    SELECT postgis_full_version();