How do I execute a query automatically in PostgreSQL when connecting via shell? How do I execute a query automatically in PostgreSQL when connecting via shell? shell shell

How do I execute a query automatically in PostgreSQL when connecting via shell?


psql will look for, and execute any commands found in, a couple of places every time it starts up. One is the system-wide psqlrc file, and one is in the home-directory of the login that's running psql, ~/.psqlrc.

So, you could add the SET command that you always want to be run, to your .psqlrc file, and it'll get executed every time you start up. See the example below:

~ $ cat ~/.psqlrcSET search_path='mything';~ $ psqlSET search_path='mything';SETpsql (8.4.20, server 9.2.10)WARNING: psql version 8.4, server version 9.2.     Some psql features might not work.Type "help" for help.rhnschema=# show search_path;search_path -------------mything(1 row)rhnschema=#