為 AlloyDB Omni 安裝 PostGIS

選取說明文件版本:

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

事前準備

確認您已在系統上安裝 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: 15.5.5
    

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

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

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

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

    Docker

       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 google/alloydbomni:${OMNI_VERSION}
    
       COPY --from=postgres /usr/lib/postgresql/15/lib/postgis-3.so /usr/lib/postgresql/15/lib/postgis-3.so
       COPY --from=postgres /usr/lib/postgresql/15/lib/postgis_sfcgal-3.so /usr/lib/postgresql/15/lib/postgis_sfcgal-3.so
       COPY --from=postgres /usr/lib/postgresql/15/lib/postgis_raster-3.so /usr/lib/postgresql/15/lib/postgis_raster-3.so
       COPY --from=postgres /usr/lib/postgresql/15/lib/postgis_topology-3.so /usr/lib/postgresql/15/lib/postgis_topology-3.so
       COPY --from=postgres /usr/lib/postgresql/15/lib/address_standardizer-3.so /usr/lib/postgresql/15/lib/address_standardizer-3.so
       COPY --from=postgres /usr/share/postgresql/15/extension/address_standardizer* /usr/lib/postgresql/15/share/extension/
       COPY --from=postgres /usr/share/postgresql/15/extension/postgis* /usr/lib/postgresql/15/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 google/alloydbomni-with-postgis:$OMNI_VERSION .

    Podman

       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 google/alloydbomni:${OMNI_VERSION}
    
       COPY --from=postgres /usr/lib/postgresql/15/lib/postgis-3.so /usr/lib/postgresql/15/lib/postgis-3.so
       COPY --from=postgres /usr/lib/postgresql/15/lib/postgis_sfcgal-3.so /usr/lib/postgresql/15/lib/postgis_sfcgal-3.so
       COPY --from=postgres /usr/lib/postgresql/15/lib/postgis_raster-3.so /usr/lib/postgresql/15/lib/postgis_raster-3.so
       COPY --from=postgres /usr/lib/postgresql/15/lib/postgis_topology-3.so /usr/lib/postgresql/15/lib/postgis_topology-3.so
       COPY --from=postgres /usr/lib/postgresql/15/lib/address_standardizer-3.so /usr/lib/postgresql/15/lib/address_standardizer-3.so
       COPY --from=postgres /usr/share/postgresql/15/extension/address_standardizer* /usr/lib/postgresql/15/share/extension/
       COPY --from=postgres /usr/share/postgresql/15/extension/postgis* /usr/lib/postgresql/15/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 podman build --build-arg OMNI_VERSION=$OMNI_VERSION --tag google/alloydbomni-with-postgis:$OMNI_VERSION .

    Docker

       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 google/alloydbomni:${OMNI_VERSION}
    
       COPY --from=postgres /usr/lib/postgresql/15/lib/postgis-3.so /usr/lib/postgresql/15/lib/postgis-3.so
       COPY --from=postgres /usr/lib/postgresql/15/lib/postgis_sfcgal-3.so /usr/lib/postgresql/15/lib/postgis_sfcgal-3.so
       COPY --from=postgres /usr/lib/postgresql/15/lib/postgis_raster-3.so /usr/lib/postgresql/15/lib/postgis_raster-3.so
       COPY --from=postgres /usr/lib/postgresql/15/lib/postgis_topology-3.so /usr/lib/postgresql/15/lib/postgis_topology-3.so
       COPY --from=postgres /usr/lib/postgresql/15/lib/address_standardizer-3.so /usr/lib/postgresql/15/lib/address_standardizer-3.so
       COPY --from=postgres /usr/share/postgresql/15/extension/address_standardizer* /usr/lib/postgresql/15/share/extension/
       COPY --from=postgres /usr/share/postgresql/15/extension/postgis* /usr/lib/postgresql/15/share/extension/
    
       COPY --from=postgres /usr/lib/aarch64-linux-gnu/ /usr/lib/aarch64-linux-gnu/
       COPY --from=postgres /lib/aarch64-linux-gnu/ /lib/aarch64-linux-gnu/
    
       COPY --from=postgres /usr/lib/aarch64-linux-gnu/liblapack.so.3 /usr/lib/
       COPY --from=postgres /usr/lib/aarch64-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 google/alloydbomni-with-postgis:$OMNI_VERSION .

    Podman

       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 google/alloydbomni:${OMNI_VERSION}
    
       COPY --from=postgres /usr/lib/postgresql/15/lib/postgis-3.so /usr/lib/postgresql/15/lib/postgis-3.so
       COPY --from=postgres /usr/lib/postgresql/15/lib/postgis_sfcgal-3.so /usr/lib/postgresql/15/lib/postgis_sfcgal-3.so
       COPY --from=postgres /usr/lib/postgresql/15/lib/postgis_raster-3.so /usr/lib/postgresql/15/lib/postgis_raster-3.so
       COPY --from=postgres /usr/lib/postgresql/15/lib/postgis_topology-3.so /usr/lib/postgresql/15/lib/postgis_topology-3.so
       COPY --from=postgres /usr/lib/postgresql/15/lib/address_standardizer-3.so /usr/lib/postgresql/15/lib/address_standardizer-3.so
       COPY --from=postgres /usr/share/postgresql/15/extension/address_standardizer* /usr/lib/postgresql/15/share/extension/
       COPY --from=postgres /usr/share/postgresql/15/extension/postgis* /usr/lib/postgresql/15/share/extension/
    
       COPY --from=postgres /usr/lib/aarch64-linux-gnu/ /usr/lib/aarch64-linux-gnu/
       COPY --from=postgres /lib/aarch64-linux-gnu/ /lib/aarch64-linux-gnu/
    
       COPY --from=postgres /usr/lib/aarch64-linux-gnu/liblapack.so.3 /usr/lib/
       COPY --from=postgres /usr/lib/aarch64-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 podman 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

    docker exec -it my-omni-postgis psql -h localhost -U postgres

    Podman

    podman exec -it my-omni-postgis psql -h localhost -U postgres
  6. 啟用 PostGIS:
    CREATE EXTENSION IF NOT EXISTS POSTGIS;
    
    SELECT postgis_full_version();
    輸出內容如下所示:
    postgis_full_version
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    POSTGIS="3.4.2 c19ce56" [EXTENSION] PGSQL="150" GEOS="3.11.1-CAPI-1.17.1" PROJ="9.1.1 NETWORK_ENABLED=OFF URL_ENDPOINT= USER_WRITABLE_DIRECTORY=/var/lib/postgresql/.local/share/proj DATABASE_PATH=/usr/share/proj/proj.db" LIBXML="2.9.14" LIBJSON="0.16" LIBPROTOBUF="1.4.1" WAGYU="0.5.0 (Internal)"
    (1 row)