为 AlloyDB Omni 安装 PostGIS

选择文档版本:

本页面介绍如何手动向现有 AlloyDB Omni 安装添加 PostGIS 扩展程序。借助 PostGIS 扩展程序,您可以存储地理空间数据、将其编入索引以及查询地理空间数据。

您可以使用以下任一方法安装 PostGIS:

  • 在 Docker 或 Podman CLI 中使用 Debian 选项。Docker 依赖于守护程序,并且大多数操作都需要根权限,而 Podman 是无守护程序且无根的。
  • 使用 Red Hat 通用基础映像 (UBI)。UBI 容器基础操作系统映像基于 Red Hat Enterprise Linux (RHEL) 的部分内容构建。

准备工作

在系统上安装 AlloyDB Omni

将 PostGIS 添加到 AlloyDB Omni 安装

如需将 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: 16.3.0
    

    记下 AlloyDB Omni 版本号,因为您在下一步中需要用到它。

  2. 设置 OMNI_VERSION 环境变量:

    OMNI_VERSION=VERSION
    

    VERSION 替换为来自上一步的完整数据库服务器版本,例如 16.3.0

  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-16-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-16-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. 创建一个名为 my-omni-postgis 的 AlloyDB Omni 容器:

    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)