Step 7 - Using PGD CLI v4

Using PGD CLI

The PGD CLI client uses a configuration file to work out which hosts to connect to. There are options that allow you to override this to use alternative configuration files or explicitly point at a server, but by default PGD CLI looks for a configuration file in preset locations.

The connection to the database is authenticated in the same way as other command line utilities (like the psql command) are authenticated.

Unlike other commands, PGD CLI doesn't interactively prompt for your password. Therefore, you must pass your password using one of the following methods:

  • Adding an entry to your .pgpass password file, which includes the host, port, database name, user name, and password.
  • Setting the password in the PGPASSWORD environment variable.
  • Including the password in the connection string.

We recommend the first option, as the other options don't scale well with multiple database clusters, or they compromise password confidentiality.

Configuring and connecting PGD CLI

  • Ensure PGD-CLI is installed.
    • If PGD CLI is already installed, move to the next step.
    • For any system, repeat the configure repositories step on that system.
    • Then run the package installation command appropriate for that platform:
      • RHEL and derivatives: sudo dnf install edb-pgd-cli
      • Debian, Ubuntu, and derivatives: sudo apt-get install edb-pgd-cli
  • Create a configuration file.
    • YAML file that specifies the cluster and endpoints for the PGD CLI application to use.
  • Install the configuration file.
    • Copy the YAML configuration file to a default config directory /etc/edb/ as pgd-config.yml.
    • Repeat this step on any system where you want to run PGD CLI.
  • Add /usr/local/bin/ to the PATH for any user wanting to use PGD CLI.
    • Add /usr/local/bin to the path in your .bashrc file
  • Run pgd-cli with the pgd command.

Use PGD CLI to explore the cluster

  • Check the health of the cluster with the check-health command.
  • Show the nodes in the cluster with the show-nodes command.

More details of these command are shown in the worked example that follows.

Also consult the PGD CLI documentation for details of other configuration options and a full command reference.

Worked example

Ensure PGD CLI is installed

This worked example configures and uses PGD CLI on host-one, where Postgres and PGD are already installed. You don't need to install PGD CLI again. The example uses the enterprisedb account as this is already configured for access to Postgres. If you aren't logged in as enterprisedb switch to it:

sudo -iu enterprisedb

Create a configuration file

The PGD CLI configuration file is similar to the PGD Proxy configuration file. It's a YAML file that contains a cluster object. This has two properties:

  • The name of the PGD cluster's top-level group (as name)
  • An array of endpoints of databases (as endpoints)
cluster:
  name: pgd
  endpoints:
    - host=host-one dbname=bdrdb port=5444
    - host=host-two dbname=bdrdb port=5444
    - host=host-three dbname=bdrdb port=5444

Note that the endpoints in this example specify port=5444. This is necessary for EDB Postgres Advanced Server instances. For EDB Postgres Extended and community PostgreSQL, this can be set to port=5432.

Install the configuration file

Create the PGD CLI configuration directory:

sudo mkdir -p /etc/edb/

Then, write the configuration to the pgd-config.yml file in the /etc/edb/ directory.

For this example, this can be run on host-one to create the file:

cat <<EOF | sudo tee /etc/edb/pgd-config.yml
cluster:
  name: pgd
  endpoints:
    - host=host-one dbname=bdrdb port=5444
    - host=host-two dbname=bdrdb port=5444
    - host=host-three dbname=bdrdb port=5444
EOF

This process can be repeated on any system where you need to use PGD CLI.

Add /usr/local/bin to the Path

To add the directory where the pgd command is installed to your path, run:

export PATH=/usr/local/bin:$PATH

For users other than enteprisedb, add this line to your .bashrc file to always have pgd available.

For the enterprisedb user, add the line to /var/lib/ebd/.enterprisedb_profile.

Run PGD CLI

With the configuration file in place, and logged is as the enterprisedb system user, you can run pgd-cli. For example, you can use the show-nodes command to list the nodes in your cluster and their status. For example:

pgd show-nodes
Output
Node       Node ID    Group Type Current State Target State Status Seq ID
----       -------    ----- ---- ------------- ------------ ------ ------
node-one   2824718320 dc1   data ACTIVE        ACTIVE       Up     1
node-three 1954860017 dc1   data ACTIVE        ACTIVE       Up     3
node-two   2299992455 dc1   data ACTIVE        ACTIVE       Up     2

Using PGD CLI to explore the cluster

Once it's configured, you can use PGD CLI to get PGD-level views of the cluster.

Check the health of the cluster

The check-health command provides a quick way to view the health of the cluster:

pgd check-health
Output
Check      Status Message
-----      ------ -------
ClockSkew  Ok     All BDR node pairs have clockskew within permissible limit
Connection Ok     All BDR nodes are accessible
Raft       Ok     Raft Consensus is working correctly
Replslots  Ok     All BDR replication slots are working correctly
Version    Ok     All nodes are running same BDR versions

Show the nodes in the cluster

As previously seen, the show-nodes command lists the nodes in the cluster:

pgd show-nodes
Output
Node       Node ID    Group Type Current State Target State Status Seq ID
----       -------    ----- ---- ------------- ------------ ------ ------
node-one   2824718320 dc1   data ACTIVE        ACTIVE       Up     1
node-three 1954860017 dc1   data ACTIVE        ACTIVE       Up     3
node-two   2299992455 dc1   data ACTIVE        ACTIVE       Up     2

This view shows which group the node is a member of and its current status. To find out what versions of PGD and Postgres are running on the nodes, use show-version:

pgd show-version
Output
----       ----------- ----------------
node-one   4.3.3       14.11.0
node-two   4.3.3       14.11.0
node-three 4.3.3       14.11.0