Select a documentation version: <button class="button button-with-icon cloud-select-dropdown__button button-primary" aria-haspopup="true">16.3.0</button>

- [Current (18.4.0)](https://docs.cloud.google.com/alloydb/omni/kubernetes/current/docs/install-postgis.md.txt)
- [18.4.0](https://docs.cloud.google.com/alloydb/omni/kubernetes/18.4.0/docs/install-postgis.md.txt)
- [18.3.0](https://docs.cloud.google.com/alloydb/omni/kubernetes/18.3.0/docs/install-postgis.md.txt)
- [18.1.0](https://docs.cloud.google.com/alloydb/omni/kubernetes/18.1.0/docs/install-postgis.md.txt)
- [17.10.0](https://docs.cloud.google.com/alloydb/omni/kubernetes/17.10.0/docs/install-postgis.md.txt)
- [17.9.0](https://docs.cloud.google.com/alloydb/omni/kubernetes/17.9.0/docs/install-postgis.md.txt)
- [17.7.0](https://docs.cloud.google.com/alloydb/omni/kubernetes/17.7.0/docs/install-postgis.md.txt)
- [17.5.0](https://docs.cloud.google.com/alloydb/omni/kubernetes/17.5.0/docs/install-postgis.md.txt)
- [16.14.0](https://docs.cloud.google.com/alloydb/omni/kubernetes/16.14.0/docs/install-postgis.md.txt)
- [16.13.0](https://docs.cloud.google.com/alloydb/omni/kubernetes/16.13.0/docs/install-postgis.md.txt)
- [16.11.0](https://docs.cloud.google.com/alloydb/omni/kubernetes/16.11.0/docs/install-postgis.md.txt)
- [16.9.0](https://docs.cloud.google.com/alloydb/omni/kubernetes/16.9.0/docs/install-postgis.md.txt)
- [16.8.0](https://docs.cloud.google.com/alloydb/omni/kubernetes/16.8.0/docs/install-postgis.md.txt)
- [16.3.0](https://docs.cloud.google.com/alloydb/omni/kubernetes/16.3.0/docs/install-postgis.md.txt)
- [15.7.1](https://docs.cloud.google.com/alloydb/omni/kubernetes/15.7.1/docs/install-postgis.md.txt)
- [15.7.0](https://docs.cloud.google.com/alloydb/omni/kubernetes/15.7.0/docs/install-postgis.md.txt)
- [15.5.5](https://docs.cloud.google.com/alloydb/omni/kubernetes/15.5.5/docs/install-postgis.md.txt)
- [15.5.4](https://docs.cloud.google.com/alloydb/omni/kubernetes/15.5.4/docs/install-postgis.md.txt)
- [15.5.2](https://docs.cloud.google.com/alloydb/omni/kubernetes/15.5.2/docs/install-postgis.md.txt)

<br />

<button value="debian" default="">Debian</button> <button value="ubi">UBI</button>

<br />

To store, index, and query geospatial data in AlloyDB Omni, you can
manually add the [PostGIS extension](https://postgis.net/) to your
installation.

You can install PostGIS by using either of the following methods:

- Use Debian options in the Docker or Podman CLI. Docker relies on a daemon and requires root privileges for most operations, while Podman is daemonless and rootless.
- Use Red Hat Universal Base Images (UBI). UBI container base operating system images are built from parts of Red Hat Enterprise Linux (RHEL).

## Before you begin

[Install the AlloyDB Omni Kubernetes operator](https://docs.cloud.google.com/alloydb/omni/kubernetes/16.3.0/docs/deploy-kubernetes.md.txt#install) on
your Kubernetes cluster.

## Add PostGIS to your AlloyDB Omni installation

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

1. Set the `OMNI_VERSION` environment variable:

   ```bash
   OMNI_VERSION=VERSION
   ```

   Replace `VERSION` with the complete
   AlloyDB Omni database server version---for example,
   `16.3.0`.
2. Build a custom Docker image that includes the PostGIS extension:

   ```bash
   mkdir -p ~/alloydb-omni-postgis

   tee ~/alloydb-omni-postgis/Dockerfile << 'EOF'
   ARG OMNI_VERSION
   FROM google/alloydbomni:${OMNI_VERSION}
   RUN apt-get update && \
         apt-get install -y --no-install-recommends \
         postgresql-16-postgis-3 \
         postgresql-16-address-standardizer \
         postgresql-16-postgis-tiger-geocoder && \
         apt-get purge -y --auto-remove && \
         rm -rf /var/lib/apt/lists/*
   EOF

   cd ~/alloydb-omni-postgis

   sudo docker build --build-arg OMNI_VERSION=$OMNI_VERSION \
     --tag google/alloydbomni-with-postgis:$OMNI_VERSION .
   ```
3. Tag and push your custom PostGIS image to a container registry that your
   Kubernetes cluster can access:

   ```bash
   sudo docker tag google/alloydbomni-with-postgis:$OMNI_VERSION \
     CUSTOM_IMAGE_URI
   sudo docker push CUSTOM_IMAGE_URI
   ```

   Replace `CUSTOM_IMAGE_URI` with the target image URI
   in your container registry---for example,
   `us-central1-docker.pkg.dev/my-project/my-repo/alloydbomni-with-postgis:16.3.0`.
4. Create a manifest file named `db-cluster.yaml` that specifies your custom
   PostGIS image in the `databaseImage` field of the `DBCluster` resource:

   ```yaml
   apiVersion: v1
   kind: Secret
   metadata:
     name: db-pw-DB_CLUSTER_NAME
     namespace: DB_CLUSTER_NAMESPACE
   type: Opaque
   data:
     DB_CLUSTER_NAME: "ENCODED_PASSWORD"
   ---
   apiVersion: alloydbomni.dbadmin.goog/v1
   kind: DBCluster
   metadata:
     name: DB_CLUSTER_NAME
     namespace: DB_CLUSTER_NAMESPACE
   spec:
     databaseVersion: "16.3.0"
     databaseImage: "CUSTOM_IMAGE_URI"
     primarySpec:
       adminUser:
         passwordRef:
           name: db-pw-DB_CLUSTER_NAME
       resources:
         cpu: CPU_COUNT
         memory: MEMORY_SIZE
         disks:
         - name: DataDisk
           size: DISK_SIZE
   ```

   Replace the following:
   - `DB_CLUSTER_NAME`: the name of your database cluster---for example, `my-db-cluster`.
   - `DB_CLUSTER_NAMESPACE`: the namespace of your database cluster---for example, `default`.
   - `ENCODED_PASSWORD`: the base64-encoded password for the `postgres` administrator user.
   - `CUSTOM_IMAGE_URI`: the URI of the custom PostGIS image that you pushed to your container registry.
   - `CPU_COUNT`: the number of vCPUs for the primary instance---for example, `2`.
   - `MEMORY_SIZE`: the memory size for the primary instance---for example, `8Gi`.
   - `DISK_SIZE`: the storage size for the primary instance data disk---for example, `10Gi`.
5. Apply the manifest to your Kubernetes cluster:

   ```bash
   kubectl apply -f db-cluster.yaml
   ```

   For more information about the `DBCluster` resource, see
   [Create a database cluster](https://docs.cloud.google.com/alloydb/omni/kubernetes/16.3.0/docs/deploy-kubernetes.md.txt#create) and the
   [`DBCluster` custom resource reference](https://docs.cloud.google.com/alloydb/omni/kubernetes/16.3.0/docs/reference/kubernetes-crds-1.4.0/dbcluster.md.txt).
6. Connect to your database Pod by using `psql`:

   ```bash
   export DBPOD=$(kubectl get pod -n DB_CLUSTER_NAMESPACE \
     --selector=alloydbomni.internal.dbadmin.goog/dbcluster=DB_CLUSTER_NAME,alloydbomni.internal.dbadmin.goog/task-type=database \
     -o jsonpath='{.items[0].metadata.name}')

   kubectl exec -ti $DBPOD -n DB_CLUSTER_NAMESPACE \
     -c database -- psql -h localhost -U postgres
   ```
7. Enable PostGIS:

   ```sql
   CREATE EXTENSION IF NOT EXISTS POSTGIS;
   SELECT postgis_full_version();
   ```

   The output is similar to the following:

       postgis_full_version
       ---
       POSTGIS="3.3.2 4975da8" [EXTENSION] PGSQL="150" GEOS="3.11.1-CAPI-1.17.1" PROJ="9.1.1" LIBXML="2.9.14" LIBJSON="0.16" LIBPROTOBUF="1.4.1" WAGYU="0.5.0 (Internal)"
       (1 row)