Configuring the cluster
=======================

.. _configuring-the-cluster-1:

Configuring the cluster
-----------------------

The next step in the process is to configure the database and the
cluster.

This involves logging into each host and running the ``pgd`` command to
create the cluster as the database user.

These steps will vary according to which platform you are using and
which version of Postgres you are using.

Cluster name
------------

You will need to choose a name for your cluster. This is the name that
will be used to identify the cluster in the PGD CLI and in the database.
It will be referred to as ``<cluster-name>`` in the examples. If not
specified, the default name is ``pgd`` .

Group names
-----------

You will also need to choose a name for the group. This is the name that
will be used to identify the group in the PGD CLI and in the database.
It will be referred to as ``<group-name>`` in the examples.

The group name must be unique within the cluster.

Node names
----------

You will also need to choose a name for each node. This is the name that
will be used to identify the node in the PGD CLI and in the database. It
will be referred to as ``<node-name>`` in the examples. This is separate
from the host name, which is the name of the machine on which the node
is running.

The node name must be unique within the group and within the cluster.

Paths and users
---------------

The paths and users used in the examples will vary according to which
version of Postgres you are using and which platform you are using.

Select your Postgres version:

EDB Postgres Advanced Server
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Debian/Ubuntu
^^^^^^^^^^^^^

.. csv-table::
  :header: ""
  :widths: auto
  :align: left
  :class: longtable

  `enterprisedb`
  `5444`
  `/usr/lib/edb-as/$PG_VERSION/bin/`
  `/var/lib/edb-as/$PG_VERSION/main/`

.. code:: shell

   sudo -iu enterprisedb
   export PG_VERSION=<version>
   export PATH=$PATH:/usr/lib/edb-as/$PG_VERSION/bin/
   export PGDATA=/var/lib/edb-as/$PG_VERSION/main/
   export PGPORT=5444

CentOS/RHEL
^^^^^^^^^^^

.. csv-table::
  :header: ""
  :widths: auto
  :align: left
  :class: longtable

  `enterprisedb`
  `5444`
  `/usr/edb/as$PG_VERSION/bin/`
  `/var/lib/edb/as$PG_VERSION/data/`

.. code:: shell

   sudo -iu enterprisedb
   export PG_VERSION=<version>
   export PATH=$PATH:/usr/edb/as$PG_VERSION/bin/
   export PGDATA=/var/lib/edb/as$PG_VERSION/data/
   export PGPORT=5444

EDB Postgres Extended
^^^^^^^^^^^^^^^^^^^^^

.. _debianubuntu-1:

Debian/Ubuntu
^^^^^^^^^^^^^

.. csv-table::
  :header: ""
  :widths: auto
  :align: left
  :class: longtable

  `postgres`
  `5432`
  `/usr/lib/edb-pge/$PG_VERSION/bin/`
  `/var/lib/edb-pge/$PG_VERSION/main/`

.. code:: shell

   sudo -iu postgres
   export PG_VERSION=<version>
   export PATH=$PATH:/usr/lib/edb-pge/$PG_VERSION/bin/
   export PGDATA=/var/lib/edb-pge/$PG_VERSION/main/
   export PGPORT=5432

.. _centosrhel-1:

CentOS/RHEL
^^^^^^^^^^^

.. csv-table::
  :header: ""
  :widths: auto
  :align: left
  :class: longtable

  `postgres`
  `5432`
  `/usr/edb/pge$PG_VERSION/bin/`
  `/var/lib/edb-pge/$PG_VERSION/data/`

.. code:: shell

   sudo -iu postgres
   export PG_VERSION=<version>
   export PATH=$PATH:/usr/edb/pge$PG_VERSION/bin/
   export PGDATA=/var/lib/edb-pge/$PG_VERSION/data/
   export PGPORT=5432

Community PostgreSQL
^^^^^^^^^^^^^^^^^^^^

.. csv-table::
  :header: ""
  :widths: auto
  :align: left
  :class: longtable

  `postgres`
  `5432`
  `/usr/lib/postgresql/$PG_VERSION/bin/`
  `/var/lib/postgresql/$PG_VERSION/main/`

.. code:: shell

   sudo -iu postgres
   export PG_VERSION=<version>
   export PATH=$PATH:/usr/lib/postgresql/$PG_VERSION/bin/
   export PGDATA=/var/lib/postgresql/$PG_VERSION/main/
   export PGPORT=5432

.. _centosrhel-2:

CentOS/RHEL
^^^^^^^^^^^

.. csv-table::
  :header: ""
  :widths: auto
  :align: left
  :class: longtable

  `postgres`
  `5432`
  `/usr/pgsql-$PG_VERSION/bin/`
  `/var/lib/pgsql/$PG_VERSION/data/`

.. code:: shell

   sudo -iu postgres
   export PG_VERSION=<version>
   export PATH=$PATH:/usr/pgsql-$PG_VERSION/bin/
   export PGDATA=/var/lib/pgsql/$PG_VERSION/data/
   export PGPORT=5432

On each host
------------

Run the commands from the script/settings above to set the environment
variables and paths for the Postgres user on each host. This will ensure
that the ``pgd`` command can find the Postgres executable files and data
directory.

1. Using the appropriate user, log in as the database user.

.. code:: bash

   sudo -iu <db-user>

1. Set the Postgres version environment variable. Don’t forget to
   replace ``<version>`` with the actual version number you are using,
   such as ``17`` .

.. code:: bash

   export PG_VERSION=<version>

1. Add the Postgres executable files to your path.

.. code:: bash

   export PATH=$PATH:<executable-path>

1. Set the Postgres data directory environment variable.

.. code:: bash

   export PGDATA=<data-directory>

1. Set the Postgres password environment variable. Don’t forget to
   replace ``<db-password>`` with the actual password you want for the
   database user.

.. code:: bash

   export PGPASSWORD=<db-password>

On the first host
^^^^^^^^^^^^^^^^^

The first host in the cluster is also the first node and will be where
we begin the cluster creation. On the first host, run the following
command to create the cluster:

.. code:: bash

   pgd node <first-node-name> setup --dsn "host=<first-host> user=<db-user> port=<db-port> dbname=<dbname>" --group-name <group-name>

This command will create the data directory and initialize the database,
then will create the cluster and the group on the first node.

On the second host
^^^^^^^^^^^^^^^^^^

On the second host, run the following command to create the cluster:

.. code:: bash

   pgd node <second-node-name> setup --dsn "host=<second-host> user=<db-user> port=<db-port> dbname=<db-name>" --cluster-dsn "host=<first-host> user=<db-user> port=<db-port> dbname=<db-name>"

This command will create the node on the second host, and then join the
cluster using the cluster-dsn setting to connect to the first host.

On the third host
^^^^^^^^^^^^^^^^^

On the third host, run the following command to create the cluster:

.. code:: bash

   pgd node <third-node-name> setup --dsn "host=<third-host> user=<db-user> port=<db-port> dbname=<db-name>" --cluster-dsn "host=<first-host> user=<db-user> port=<db-port> dbname=<db-name>"

This command will create the node on the third host, and then join the
cluster using the cluster-dsn setting to connect to the first host.

Worked example
--------------

In this example, we will configure the PGD cluster with EDB Postgres
Extended Server 17 on a CentOS/RHEL system that we :ref:`Configuring PGD repositories <Configuring PGD repositories>`  and
:ref:`Installing the database and pgd <Installing the database and pgd>`  in the previous steps.

We will now create a cluster called ``pgd`` with three nodes called
``node-1`` , ``node-2`` , and ``node-3`` .

- The group name will be ``group-1`` . The hosts are ``host-1`` ,
  ``host-2`` , and ``host-3`` .

- The Postgres version is 17.

- The database user is ``postgres`` .

- The database port is 5432.

- The database name is ``pgddb`` .

- The Postgres executable files are in ``/usr/edb/pge17/bin/`` .

- The Postgres data directory is in ``/var/lib/edb-pge/17/main/`` .

- The Postgres password is ``secret`` .

(Note that we assume the Postgres version environment variable
PG_VERSION is set to ``17`` from the previous step, and that we are
preserving the environment variable when switching users.)

.. _on-the-first-host-1:

On the first host
^^^^^^^^^^^^^^^^^

.. code:: bash

   sudo  -iu postgres
   export PG_VERSION=17
   export PATH=$PATH:/usr/edb/pge$PG_VERSION/bin/
   export PGDATA=/var/lib/edb-pge/$PG_VERSION/data/
   export PGPASSWORD=secret
   pgd node node-1 setup --dsn "host=host-1 user=postgres port=5432 dbname=pgddb" --group-name group-1

.. _on-the-second-host-1:

On the second host
^^^^^^^^^^^^^^^^^^

.. code:: bash

   sudo -iu postgres
   export PG_VERSION=17
   export PATH=$PATH:/usr/edb/pge$PG_VERSION/bin/
   export PGDATA=/var/lib/edb-pge/$PG_VERSION/data/
   export PGPASSWORD=secret
   pgd node node-2 setup --dsn "host=host-2 user=postgres port=5432 dbname=pgddb" --cluster-dsn "host=host-1 user=postgres port=5432 dbname=pgddb"

.. _on-the-third-host-1:

On the third host
^^^^^^^^^^^^^^^^^

.. code:: bash

   sudo -iu postgres
   export PG_VERSION=17
   export PATH=$PATH:/usr/edb/pge$PG_VERSION/bin/
   export PGDATA=/var/lib/edb-pge/$PG_VERSION/data/
   export PGPASSWORD=secret
   pgd node node-3 setup --dsn "host=host-3 user=postgres port=5432 dbname=pgddb" --cluster-dsn "host=host-1 user=postgres port=5432 dbname=pgddb"

The next step is to :ref:`Checking the cluster <Checking the cluster>`  .
