Home Explore Blog CI



postgresql

37th chunk of `doc/src/sgml/libpq.sgml`
c58eba9bb7f21555f1d97bea1bc15f6743724e830914b7a80000000100000fa3
 abstraction.  Use the accessor
    functions described below to get at the contents of <structname>PGconn</structname>.
    Reference to internal <structname>PGconn</structname> fields using
    <filename>libpq-int.h</filename> is not recommended because they are subject to change
    in the future.
   </para>
  </tip>

  <para>
   The following functions return parameter values established at connection.
   These values are fixed for the life of the connection.  If a multi-host
   connection string is used, the values of <xref linkend="libpq-PQhost"/>,
   <xref linkend="libpq-PQport"/>, and <xref linkend="libpq-PQpass"/> can change if a new connection
   is established using the same <structname>PGconn</structname> object.  Other values
   are fixed for the lifetime of the <structname>PGconn</structname> object.

   <variablelist>
    <varlistentry id="libpq-PQdb">
     <term><function>PQdb</function><indexterm><primary>PQdb</primary></indexterm></term>

     <listitem>
      <para>
       Returns the database name of the connection.
<synopsis>
char *PQdb(const PGconn *conn);
</synopsis>
      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="libpq-PQuser">
     <term><function>PQuser</function><indexterm><primary>PQuser</primary></indexterm></term>

     <listitem>
      <para>
       Returns the user name of the connection.
<synopsis>
char *PQuser(const PGconn *conn);
</synopsis>
      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="libpq-PQpass">
     <term><function>PQpass</function><indexterm><primary>PQpass</primary></indexterm></term>

     <listitem>
      <para>
       Returns the password of the connection.
<synopsis>
char *PQpass(const PGconn *conn);
</synopsis>
      </para>

      <para>
       <xref linkend="libpq-PQpass"/> will return either the password specified
       in the connection parameters, or if there was none and the password
       was obtained from the <link linkend="libpq-pgpass">password
       file</link>, it will return that.  In the latter case,
       if multiple hosts were specified in the connection parameters, it is
       not possible to rely on the result of <xref linkend="libpq-PQpass"/> until
       the connection is established.  The status of the connection can be
       checked using the function <xref linkend="libpq-PQstatus"/>.
      </para>
     </listitem>
    </varlistentry>

    <varlistentry id="libpq-PQhost">
     <term><function>PQhost</function><indexterm><primary>PQhost</primary></indexterm></term>

     <listitem>
      <para>
       Returns the server host name of the active connection.
       This can be a host name, an IP address, or a directory path if the
       connection is via Unix socket.  (The path case can be distinguished
       because it will always be an absolute path, beginning
       with <literal>/</literal>.)
<synopsis>
char *PQhost(const PGconn *conn);
</synopsis>
      </para>

      <para>
       If the connection parameters specified both <literal>host</literal> and
       <literal>hostaddr</literal>, then <xref linkend="libpq-PQhost"/> will
       return the <literal>host</literal> information.  If only
       <literal>hostaddr</literal> was specified, then that is returned.
       If multiple hosts were specified in the connection parameters,
       <xref linkend="libpq-PQhost"/> returns the host actually connected to.
      </para>

      <para>
       <xref linkend="libpq-PQhost"/> returns <symbol>NULL</symbol> if the
       <parameter>conn</parameter> argument is <symbol>NULL</symbol>.
       Otherwise, if there is an error producing the host information (perhaps
       if the connection has not been fully established or there was an
       error), it returns an empty string.
      </para>

      <para>
       If multiple hosts were specified in the connection parameters, it is
       not possible to rely on the result of <xref linkend="libpq-PQhost"/> until
       the connection is established.  The status

Title: Connection Status Functions: Retrieving Connection Parameters
Summary
This section describes functions used to retrieve connection parameter values established at the time of connection. These functions, including PQdb, PQuser, PQpass, and PQhost, return information such as the database name, user name, password, and server host name of the active connection. It is recommended to use these accessor functions to maintain the PGconn abstraction. The values of PQhost, PQport, and PQpass can change if a new connection is established using the same PGconn object in a multi-host connection string.