I have a need to send banner messages to a psql client that I can set
on the server and will be displayed on any psql client that connects
to the database. This would be mostly used as an additional indicator
to which database you are connecting, but could also be used by people
to force their users to see an security message when connecting to the
database. The attached patch will allow you to execute

ALTER DATABASE postgres SET
client_message=E'********************************************************************************\nBEWARE:
You are connecting to a production database. If you do anything to\n
     bring this server down, you will be destroyed by your supreme
overlord.\n********************************************************************************\n';

And then when you connect to psql, you will see:

[e3@workstation bin]$ ./psql -U user1 postgres
psql (9.2devel)
********************************************************************************
BEWARE: You are connecting to a production database. If you do anything to
        bring this server down, you will be destroyed by your supreme overlord.
********************************************************************************

Type "help" for help.

postgres=>


Any feedback is welcome.

Thanks
diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
new file mode 100644
index 0cc3296..fa5b942
*** a/doc/src/sgml/config.sgml
--- b/doc/src/sgml/config.sgml
*************** dynamic_library_path = 'C:\tools\postgre
*** 5323,5328 ****
--- 5323,5341 ----
        </listitem>
       </varlistentry>
  
+      <varlistentry id="guc-client-message" xreflabel="client_message">
+       <term><varname>client_message</varname> (<type>string</type>)</term>
+       <indexterm>
+        <primary><varname>client_message</> configuration parameter</primary>
+       </indexterm>
+       <listitem>
+        <para>
+         The <varname>client_message</varname> can be any string that will be 
+         displayed to the user in the banner of psql. 
+        </para>
+       </listitem>
+      </varlistentry>
+ 
       </variablelist>
      </sect2>
     </sect1>
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
new file mode 100644
index 5c910dd..58b6000
*** a/src/backend/utils/misc/guc.c
--- b/src/backend/utils/misc/guc.c
*************** static char *log_destination_string;
*** 455,460 ****
--- 455,461 ----
  static char *syslog_ident_str;
  static bool phony_autocommit;
  static bool session_auth_is_superuser;
+ static char *client_message_string;
  static double phony_random_seed;
  static char *client_encoding_string;
  static char *datestyle_string;
*************** static struct config_string ConfigureNam
*** 3018,3023 ****
--- 3019,3035 ----
  		check_application_name, assign_application_name, NULL
  	},
  
+         {
+                 {"client_message", PGC_USERSET, CLIENT_CONN_OTHER,
+                         gettext_noop("Sets a message to be displayed to the user when connecting via psql."),
+                         NULL,
+                         GUC_REPORT | GUC_NO_SHOW_ALL
+                 },
+                 &client_message_string,
+                 "",
+                 NULL, NULL, NULL
+         },
+ 
  	/* End-of-list marker */
  	{
  		{NULL, 0, 0, NULL, NULL}, NULL, NULL, NULL, NULL, NULL
diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample
new file mode 100644
index 315db46..8eb5af5
*** a/src/backend/utils/misc/postgresql.conf.sample
--- b/src/backend/utils/misc/postgresql.conf.sample
***************
*** 515,520 ****
--- 515,521 ----
  
  #dynamic_library_path = '$libdir'
  #local_preload_libraries = ''
+ #client_message = ''
  
  
  #------------------------------------------------------------------------------
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
new file mode 100644
index 69fac83..44bf114
*** a/src/bin/psql/command.c
--- b/src/bin/psql/command.c
*************** static bool get_create_function_cmd(PGco
*** 65,70 ****
--- 65,71 ----
  static int	strip_lineno_from_funcdesc(char *func);
  static void minimal_error_message(PGresult *res);
  
+ static void printClientMessage(void);
  static void printSSLInfo(void);
  
  #ifdef WIN32
*************** connection_warnings(bool in_startup)
*** 1699,1709 ****
--- 1700,1729 ----
  		checkWin32Codepage();
  #endif
  		printSSLInfo();
+ 
+ 		printClientMessage();
  	}
  }
  
  
  /*
+  * printClientMessage
+  *
+  * Prints any message stored in the client_message GUC
+  */
+ static void
+ printClientMessage(void)
+ {
+ 	const char *message;
+ 
+ 	message = PQparameterStatus(pset.db, "client_message");
+ 
+ 	if (message)
+ 		printf(_("%s\n"), message);
+ }
+ 
+ 
+ /*
   * printSSLInfo
   *
   * Prints information about the current SSL connection, if SSL is in use
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to