Initializing PGD
================

Once the seed node is upgraded to the target Postgres version, you must
install the Bi-Directional Replication (BDR) extension and initialize
the cluster to transition the node into a distributed environment.

Installing and configuring PGD
------------------------------

The seed node requires PGD packages that specifically match your
Postgres distribution and version (e.g., ``edb-pgd6-expanded-epas17``
for EPAS 17). Ensure you have configured the appropriate PGD repository
before proceeding. See `Installing the database and pgd <https://www.enterprisedb.com/docs/pgd/latest/deploying/deploy-manual/03-installing-database-and-pgd/>`_  for details.

1. To enable the BDR extension, update your Postgres configuration to
   preload the library and enable commit timestamps on the seed node:

.. code:: bash

       cat >> ${PGDATA}/postgresql.conf <<EOF
       shared_preload_libraries = bdr
       track_commit_timestamp = on
       EOF

1. Restart Postgres to load the BDR extension:

.. code:: bash

       su -u enterprisedb --command "pg_ctl restart"

Initialize the PGD cluster
--------------------------

With the extension loaded, you can now create the BDR extension within
the database you intend to replicate.

1. On the seed node, create the BDR extension:

.. code:: sql

       CREATE EXTENSION bdr;

1. Validate BDR is loaded and check the version:

.. code:: sql

       SELECT bdr.bdr_version();

1. Define the seed node as a BDR node and establish the initial node
   group that will form your cluster:

.. code:: sql

       SELECT bdr.create_node(
           node_name := ${SEED_NODE_PGD_NAME},
           dsn := ${SEED_NODE_DSN_DSN}
       );

1. Create the node group:

.. code:: sql

       SELECT bdr.create_node_group(
           node_group_name := ${PGD_CLUSTER_NAME}
       );

Configuring PGD CLI
-------------------

Use the PGD Command Line Interface (CLI) to manage and monitor the
cluster. You must create a configuration file on **all** current and
future PGD nodes that lists the connection endpoints for the entire
cluster:

.. code:: bash

   mkdir -p /etc/edb/pgd-cli
   cat <<EOF > tee /etc/edb/pgd-cli/pgd-cli-config.yml
   cluster:
     name: ${PGD_CLUSTER_NAME}
     endpoints:
       - ${SEED_NODE_DSN}
       - ${ADD_NODE_DSN}
          - ...
   EOF

Validating the PGD Installation
-------------------------------

Use the PGD CLI to verify that the single-node setup is healthy and the
CLI is correctly configured to communicate with the cluster. On the seed
node, run:

.. code:: bash

   pgd nodes list -- Expected to list exactly one node (itself).
   pgd cluster show --health -- Expected to show OK for all checks.

Next step: :ref:`Joining additional PGD nodes <Joining additional PGD nodes>`  .
