為 AlloyDB Omni 安裝 PostGIS

選取說明文件版本:

AlloyDB Omni 不包含 PostGIS 擴充功能,但您可以按照本頁的說明,手動將其新增至現有的 AlloyDB Omni 安裝項目,以便支援儲存、建立索引及查詢地理空間資料。

事前準備

確認您已在系統上安裝 AlloyDB Omni

在 AlloyDB Omni 安裝中新增 PostGIS

如要將 PostGIS 擴充功能新增至 AlloyDB Omni 安裝項目,請按照下列步驟操作:

  1. 找出已安裝的 AlloyDB Omni 版本標籤:

    Docker

    docker run --rm -it  google/alloydbomni cat VERSION.txt

    Podman

    podman run --rm -it  google/alloydbomni cat VERSION.txt

    輸出結果會與下列內容相似:

    AlloyDB Omni version: 15.7.1
    

    記下 AlloyDB Omni 版本號碼,下一個步驟會用到。

  2. 設定 OMNI_VERSION 環境變數:
    OMNI_VERSION=VERSION

    VERSION 替換為上一步的完整資料庫伺服器版本,例如 15.7.1

  3. 建立包含 PostGIS 的新 AlloyDB Omni 容器:

    Linux

           mkdir ~/alloydb-omni-postgis
           tee -a ~/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-15-postgis-3 &&
    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 .

    macOS

           mkdir ~/alloydb-omni-postgis
           tee -a ~/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-15-postgis-3 &&
    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 .
  4. 使用 AlloyDB Omni 建立名為 my-omni-postgis 的新容器:

    Docker

    docker run --name my-omni-postgis  -e POSTGRES_PASSWORD=NEW_PASSWORD  -d google/alloydbomni-with-postgis:OMNI_VERSION

    Podman

    podman run --name my-omni-postgis  -e POSTGRES_PASSWORD=NEW_PASSWORD  -d google/alloydbomni-with-postgis:OMNI_VERSION
  5. 使用 PostGIS 擴充功能連線至資料庫:

    docker exec -it my-omni-postgis psql -h localhost -U postgres
  6. 啟用 PostGIS:

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

    輸出看起來類似以下內容:

       postgres=# SELECT postgis_full_version();
       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)