I would like to present a banner motd (message of the day) to users that connect to a Postgresql database. Similar to cisco router and switch cli where you use:

banner motd "Seien Sie bitte vorsichtig....."

And then upon logging into the switch/router you see that message.

I'd like to see something similar when connecting to a database.


$ psql postgres
psql (12.0)
Type "help" for help.
Seien Sie bitte vorsichtig.....

  • 1
    Maybe add a \echo Please be careful to psqlrc? It will show before the psql (12.0) however – a_horse_with_no_name Oct 17 at 8:45
  • 1
    It looks like you’re logging in as a superuser. That’s a practice you should stop. – Colin 't Hart Oct 17 at 8:58
  • @Colin'tHart I log in as a normal user. the postgres user account does not seem to work for me – leetbacoon Oct 17 at 8:59
  • Your prompt ends with "#". On a default install this means that user is a superuser. – Colin 't Hart Oct 17 at 9:00
  • @Colin'tHart yes, but: psql -U postgres postgres -> psql: FATAL: role "postgres" does not exist – leetbacoon Oct 17 at 9:01

You cannot force that on the server side, because the server cannot control what a client displays.

But yan can do it on the client side: create a .psqlrc file in your home directory that contains something like:

\echo Seien Sie bitte vorsichtig.....

But I assume, given the text of the warning, that you want this bannet only for certain sensitive databases.

For that, you could have banner table in every database, and the statement in .psqlrc would be like:

\set QUIET on
\pset format unaligned
SELECT * FROM banner;
\pset format aligned
\set QUIET off
  • Good suggestion, thanks! – leetbacoon Oct 17 at 8:58
  • Is there a way to check the database first and print then? Like IF (SELECT current_database()) = 'postgres' THEN (SELECT * FROM banner); – leetbacoon Oct 17 at 9:26
  • 1
    Sure, you can run queries, assign the result to variables (\gset) and use \if to du different things based on the result. – Laurenz Albe Oct 17 at 10:00
  • Thank you for your help my friend – leetbacoon Oct 17 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.