為 AlloyDB Omni 安裝 PostGIS

選取說明文件版本:

本頁面說明如何手動將 PostGIS 擴充功能新增至現有的 AlloyDB Omni 安裝項目。PostGIS 擴充功能可儲存、建立索引及查詢地理空間資料。

你可以使用下列任一方法安裝 PostGIS:

  • 在 Docker 或 Podman CLI 中使用 Debian 選項。Docker 依賴精靈,且大多數作業都需要 root 權限,而 Podman 則不需要精靈和 root 權限。
  • 使用 Red Hat Universal Base Images (UBI)。UBI 容器基礎作業系統映像檔是根據 Red Hat Enterprise Linux (RHEL) 的部分內容建構而成。

事前準備

在系統上安裝 AlloyDB Omni

在 AlloyDB Omni 安裝中新增 PostGIS

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

  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: 16.8.0
    

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

  2. 設定 OMNI_VERSION 環境變數:

    OMNI_VERSION=VERSION
    

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

  3. 建構包含 PostGIS 擴充功能的自訂 Docker 映像檔:

    mkdir ~/alloydb-omni-postgis
    
    cd ~/alloydb-omni-postgis
    
    sudo dnf install -y subscription-manager
    
    sudo subscription-manager register --username EMAIL --password PASSWORD --auto-attach
    
    sudo mkdir -p entitlement rhsm-conf rhsm-ca
        sudo cp -r /etc/pki/entitlement/* entitlement/
        sudo cp -r /etc/rhsm/rhsm.conf rhsm-conf/
        sudo cp -r /etc/rhsm/ca/* rhsm-ca/
    
    cat <<EOF > Dockerfile
        FROM google/alloydbomni:16.8.0-ubi
        COPY ./entitlement /etc/pki/entitlement
        COPY ./rhsm-conf /etc/rhsm
        COPY ./rhsm-ca /etc/rhsm/ca
        RUN arch=$(uname -m) && \
            subscription-manager repos --enable codeready-builder-for-rhel-9-${arch}-rpms && \
            dnf install -y \
            https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-${arch}/pgdg-redhat-repo-latest.noarch.rpm && \
            dnf install -y postgresql16-server postgis34_16 && \
            dnf clean all
        # On RHEL-based systems, PostgreSQL extensions like Orafce and PostGIS are typically installed in /usr/pgsql-16/share/extension and /usr/pgsql-16/lib.
        # This step creates symbolic links to those files in the paths expected by AlloyDB Omni,
        # which lets AlloyDB Omni locate extension control files and shared libraries without duplicating data.
        RUN for file in /usr/pgsql-16/share/extension/*; do \
           target="/usr/lib/postgresql/16/share/extension/$(realpath -m --relative-to=/usr/pgsql-16/share/extension/ "$file")"; \
           if [ ! -e "$target" ]; then \
              ln -s "$file" "$target" || (echo "Failed to link \"$file\" to \"$target\", exiting." && exit 1); \
           else \
              echo "$target already exists"; \
           fi; \
        done && \
        for file in /usr/pgsql-16/lib/*; do \
           target="/usr/lib/postgresql/16/lib/$(realpath -m --relative-to=/usr/pgsql-16/lib/ "$file")"; \
           if [ ! -e "$target" ]; then \
              ln -s "$file" "$target" || (echo "Failed to link \"$file\" to \"$target\", exiting." && exit 1); \
           else \
              echo "$target already exists"; \
           fi; \
        done
    EOF
    

  4. 使用 AlloyDB Omni 建立名為 my-omni-postgis 的新容器:

    Docker

    docker build -t google/alloydbomni-with-postgis:latest
    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)