PostGIS
=======

`PostGIS <https://postgis.net/>`_ は、 PostgreSQLの非常に人気のオープンソース拡張機能であり、
GIS地理情報システムオブジェクトをデータベースに保存し、
SQLを介して照会するためのサポートを導入します。

..  Important::
   このセクションでは、PostGISに精通していることを前提として、EDB CloudNativePGクラスターを介してKubernetesでPostGISデータベースを使用して新しいPostgreSQLクラスターを作成する方法に関する基本的な情報を提供します。

CloudNativePGコミュニティは、公式の :ref:`PostGIS images hosted on DockerHub <PostGIS>` 
上にビルドされたコンテナイメージを維持します。詳細については、次をご覧ください。

- :ref:`postgis-containers <PostGIS>` 

- :ref:`postgis-containers <PostGIS>` 

さらに、EDBは、 PostGISを含むEDB Postgres Advanced
Serverのコンテナイメージを提供し、公式 `registry on Quay.io <https://quay.io/repository/enterprisedb/edb-postgres-advanced?tab=tags>`_ 

``-postgis`` サフィックスを使用します。

PostGISクラスターに関する基本的な概念
-------------------------------------

概念的に、PostGISベースのPostgreSQLクラスターまたは単にPostGISクラスターは、他のPostgreSQLクラスターと似ています。唯一の違いは次のとおりです。

- PostGISおよび関連ライブラリのシステム内の存在

- PostGIS拡張機能のデータベース内の存在

EDB
CloudNativePGクラスターは不変アプリケーションコンテナに基づいているため、
PostGISをプロビジョニングする唯一の方法は、オペランドに使用するコンテナイメージに追加することです。
:ref:`Container Image Requirements <Container Image Requirements>` は、これを実現する方法に関する詳細な手順を提供します。もっと簡単には、以下の例にあるように、コミュニティからのPostGISコンテナイメージを使用できます。

2番目のステップは、PostgreSQLデータベースに拡張機能をインストールすることです。これは2つの方法で行うことができます。

- アプリケーションデータベースにインストールします。これは、マイクロサービスアーキテクチャに従って、クラスターでホストするメインで唯一のデータベースです。または

- インスタンスが複数のデータベースで共有されるモノリスアーキテクチャを採用する場合、クラスター内に作成することになるすべてのデータベースで利用できるように、
  ``template1`` データベースにインストールします

..  Note::
   データベース内のマイクロサービスとモノリスアーキテクチャの詳細については、 :ref:`How many databases should be hosted in a single PostgreSQL instance? <Frequently Asked Questions (FAQ)>` を参照してください。

または :ref:`Database import <Importing Postgres databases>`  。

PostGISで新しいPostgreSQLクラスターを作成する
---------------------------------------------

PostGIS 3.2を使用して新しいPostgreSQL
14クラスターを作成することにします。

最初のステップは、オペランドに適切なPostGISコンテナイメージを使用し、
``Cluster`` リソースの\ ``.spec.imageName``
オプションを適切に設定することです。

以下は、PostGISクラスターの作成を行う方法に関するガイダンスを提供します。

::

   apiVersion: postgresql.k8s.enterprisedb.io/v1
   kind: Cluster
   metadata:
     name: postgis-example
   spec:
     instances: 3
     imageName: ghcr.io/cloudnative-pg/postgis:14
     bootstrap:
       initdb:
         postInitTemplateSQL:
           - CREATE EXTENSION postgis;
           - CREATE EXTENSION postgis_topology;
           - CREATE EXTENSION fuzzystrmatch;
           - CREATE EXTENSION postgis_tiger_geocoder;

     storage:
       size: 1Gi

..  Warning::
   EDB CloudNativePGクラスターでは構成より規約が適用されますが、運用用にシステムの構成とチューニングに時間を費やす必要があることを考慮してください。また、以下の例の`imageName` は、PostgreSQL 14の最新の利用可能なイメージを意図的にポイントしています。真の不変性を実現するには、特定のイメージ名、またはできればSHA256ダイジェストを使用する必要があります。

.. code:: yaml

   apiVersion: postgresql.k8s.enterprisedb.io/v1
   kind: Cluster
   metadata:
     name: postgis-example
   spec:
     instances: 3
     imageName: ghcr.io/enterprisedb/postgresql:16-postgis
     bootstrap:
       initdb:
         postInitTemplateSQL:
           - CREATE EXTENSION postgis;
           - CREATE EXTENSION postgis_topology;
           - CREATE EXTENSION fuzzystrmatch;
           - CREATE EXTENSION postgis_tiger_geocoder;

     storage:
       size: 1Gi

この例では、 ``app``
と呼ばれるアプリケーションデータベースを実際に作成する前に、
``template1``
データベースに対してクエリーのリストを実行する\ ``postInitTemplateSQL``
オプションに依存しています。これは、マニフェストを適用し、クラスターが起動すると、上記の拡張機能がテンプレートデータベースとアプリケーションデータベースの両方にインストールされ、使用できる状態になることを意味します。

..  Note::
   時間をかけて、 `postInitApplicationSQL` などの :ref:`API Reference <API Reference>` から`.spec.bootstrap.initdb` で使用可能なオプションを確認します。

``app``
データベースに接続することにより、コンテナ内にあるPostGISの利用可能なバージョンを簡単に確認できます。このドキュメントの値とは異なる値を取得する場合があります。

.. code:: console

   $ kubectl exec -ti postgis-example-1 -- psql app
   Defaulted container "postgres" out of: postgres, bootstrap-controller (init)
   psql (16.0 (Debian 16.0-1.pgdg110+1))
   Type "help" for help.

   app=# SELECT * FROM pg_available_extensions WHERE name ~ ^postgis ORDER BY 1;
              name           | default_version | installed_version |                          comment
   - -------------------------+-----------------+-------------------+------------------------------------------------------------
    postgis                  | 3.2.2           | 3.2.2             | PostGIS geometry and geography spatial types and functions
    postgis-3                | 3.2.2           |                   | PostGIS geometry and geography spatial types and functions
    postgis_raster           | 3.2.2           |                   | PostGIS raster types and functions
    postgis_raster-3         | 3.2.2           |                   | PostGIS raster types and functions
    postgis_sfcgal           | 3.2.2           |                   | PostGIS SFCGAL functions
    postgis_sfcgal-3         | 3.2.2           |                   | PostGIS SFCGAL functions
    postgis_tiger_geocoder   | 3.2.2           | 3.2.2             | PostGIS tiger geocoder and reverse geocoder
    postgis_tiger_geocoder-3 | 3.2.2           |                   | PostGIS tiger geocoder and reverse geocoder
    postgis_topology         | 3.2.2           | 3.2.2             | PostGIS topology spatial types and functions
    postgis_topology-3       | 3.2.2           |                   | PostGIS topology spatial types and functions
   (10 rows)

次のステップは、 ``postInitTemplateSQL``
セクションにリストされている拡張機能が\ ``app``
データベースに正しくインストールされていることを確認することです。

.. code:: console

   app=# \dx
                                           List of installed extensions
             Name          | Version |   Schema   |                        Description
   - -----------------------+---------+------------+------------------------------------------------------------
    fuzzystrmatch          | 1.1     | public     | determine similarities and distance between strings
    plpgsql                | 1.0     | pg_catalog | PL/pgSQL procedural language
    postgis                | 3.2.2   | public     | PostGIS geometry and geography spatial types and functions
    postgis_tiger_geocoder | 3.2.2   | tiger      | PostGIS tiger geocoder and reverse geocoder
    postgis_topology       | 3.2.2   | topology   | PostGIS topology spatial types and functions
   (5 rows)

最後に

.. code:: console

   app=# SELECT postgis_full_version();
                                                                               postgis_full_version
   - ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    POSTGIS="3.2.2 628da50" [EXTENSION] PGSQL="140" GEOS="3.9.0-CAPI-1.16.2" PROJ="7.2.1" LIBXML="2.9.10" LIBJSON="0.15" LIBPROTOBUF="1.3.3" WAGYU="0.5.0 (Internal)" TOPOLOGY
   (1 row)
