psql-conninfo-v3.patch

application/octet-stream

Filename: psql-conninfo-v3.patch
Type: application/octet-stream
Part: 0
Message: Re: psql \conninfo command (was: Patch: psql \whoami option)

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: unified
Series: patch v3
File+
doc/src/sgml/ref/psql-ref.sgml 10 0
src/bin/psql/command.c 16 0
src/bin/psql/help.c 2 1
src/bin/psql/tab-complete.c 1 1
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index 4f3ef5c..11a6ed2 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -781,6 +781,16 @@ testdb=>
       </varlistentry>
 
       <varlistentry>
+        <term><literal>\conninfo</literal></term>
+        <listitem>
+        <para>
+        Outputs connection information about the current database
+        connection.
+        </para>
+        </listitem>
+      </varlistentry>
+
+      <varlistentry>
         <term><literal>\copy { <replaceable class="parameter">table</replaceable> [ ( <replaceable class="parameter">column_list</replaceable> ) ] | ( <replaceable class="parameter">query</replaceable> ) }
         { <literal>from</literal> | <literal>to</literal> }
         { <replaceable class="parameter">filename</replaceable> | stdin | stdout | pstdin | pstdout }
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 35a0800..e1571b4 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -294,6 +294,22 @@ exec_command(const char *cmd,
 			free(opt);
 	}
 
+	/* \conninfo -- display information about the current connection	*/
+	else if (strcmp(cmd, "conninfo") == 0)
+	{
+		char	   *db = PQdb(pset.db);
+		char	   *host = PQhost(pset.db);
+
+		if (!db)
+			printf("Not connected.\n");
+		else if (host)
+			printf("Connected to database: \"%s\", user: \"%s\", host: \"%s\", port: %s\n",
+				   db, PQuser(pset.db), host, PQport(pset.db));
+		else
+			printf("Connected to database: \"%s\", user: \"%s\", port: %s via local domain socket\n",
+				   PQdb(pset.db), PQuser(pset.db), PQport(pset.db));
+	}
+
 	/* \copy */
 	else if (pg_strcasecmp(cmd, "copy") == 0)
 	{
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index 09a348f..19c807d 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -162,7 +162,7 @@ slashUsage(unsigned short int pager)
 {
 	FILE	   *output;
 
-	output = PageOutput(86, pager);
+	output = PageOutput(87, pager);
 
 	/* if you add/remove a line here, change the row count above */
 
@@ -249,6 +249,7 @@ slashUsage(unsigned short int pager)
 			PQdb(pset.db));
 	fprintf(output, _("  \\encoding [ENCODING]   show or set client encoding\n"));
 	fprintf(output, _("  \\password [USERNAME]   securely change the password for a user\n"));
+	fprintf(output, _("  \\conninfo              display information about current connection\n"));
 	fprintf(output, "\n");
 
 	fprintf(output, _("Operating System\n"));
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index f806a74..b43c478 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -636,7 +636,7 @@ psql_completion(char *text, int start, int end)
 	};
 
 	static const char *const backslash_commands[] = {
-		"\\a", "\\connect", "\\C", "\\cd", "\\copy", "\\copyright",
+		"\\a", "\\connect", "\\conninfo", "\\C", "\\cd", "\\copy", "\\copyright",
 		"\\d", "\\da", "\\db", "\\dc", "\\dC", "\\dd", "\\dD", "\\des", "\\deu", "\\dew", "\\df",
 		"\\dF", "\\dFd", "\\dFp", "\\dFt", "\\dg", "\\di", "\\dl",
 		"\\dn", "\\do", "\\dp", "\\drds", "\\ds", "\\dS", "\\dt", "\\dT", "\\dv", "\\du",