Determine if a user and database are available

From: Jeffrey Walton <noloader(at)gmail(dot)com>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Determine if a user and database are available
Date: 2022-09-02 21:22:12
Message-ID: CAH8yC8=fbU-DWSNso7qKDSNCj52hTop8ENKxF7_4a=hjY-nn6w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi Everyone,

I have another beginner question. I am trying to use pg_isready to
determine if a database and user are present. The program seems to
always succeed, even when I delete the user or the database.

This baffles me from the man page. I guess this explains the behavior
I am seeing.

NOTES
It is not necessary to supply correct user name, password, or database
name values to obtain the server status; however, if incorrect values
are provided, the server will log a failed connection attempt.

A typical usage is shown below, where variables are parsed from a config file.

password=$(grep 'DD_DB_Rpass' dojoConfig.yml | awk '{ print $2 }')
hostname=$(grep 'DD_DB_Host' dojoConfig.yml | awk '{ print $2 }')
database=$(grep 'DD_DB_Name' dojoConfig.yml | awk '{ print $2 }')
username=$(grep 'DD_DB_Ruser' dojoConfig.yml | awk '{ print $2 }')

PGPASSWORD=${password} pg_isready \
-h "${hostname}" -U "${username}" -d "${database}"

Given the NOTES in the man page, how do we determine if a user and
database are present using the shell? Is there another utility we
should be using?

Thanks in advance,

Jeff

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Christophe Pettus 2022-09-02 21:42:34 Re: Determine if a user and database are available
Previous Message Meera Nair 2022-09-02 16:47:24 RE: Unable to archive logs in standby server