Thread: Adding the host name to the PgSQL shell

Adding the host name to the PgSQL shell

From
Madison Kelly
Date:
Hi all,

   I work on a development and production server, and I am always
double-checking myself to make sure I am doing something on the right
server.

   Is there a way, like in terminal shells, to change the PgSQL shell's
prompt from 'db=>' to something like 'host@db=>'? I'm on PgSQL 8.1
(server on Debian) and 8.3 (devel on Ubuntu), in case it matters.

Thanks all!

Madi

Re: Adding the host name to the PgSQL shell

From
Scott Mead
Date:



On Mon, Jun 8, 2009 at 12:44 PM, Madison Kelly <linux@alteeve.com> wrote:
Hi all,

 I work on a development and production server, and I am always double-checking myself to make sure I am doing something on the right server.

 Is there a way, like in terminal shells, to change the PgSQL shell's prompt from 'db=>' to something like 'host@db=>'? I'm on PgSQL 8.1 (server on Debian) and 8.3 (devel on Ubuntu), in case it matters.

You certainly can do this, very similar to PS1 on linux with bash:

http://www.postgresql.org/docs/8.1/static/app-psql.html#APP-PSQL-PROMPTING

--Scott


Thanks all!

Madi

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Re: Adding the host name to the PgSQL shell

From
Madison Kelly
Date:
Scott Mead wrote:
>
>
>
> On Mon, Jun 8, 2009 at 12:44 PM, Madison Kelly <linux@alteeve.com
> <mailto:linux@alteeve.com>> wrote:
>
>     Hi all,
>
>      I work on a development and production server, and I am always
>     double-checking myself to make sure I am doing something on the
>     right server.
>
>      Is there a way, like in terminal shells, to change the PgSQL
>     shell's prompt from 'db=>' to something like 'host@db=>'? I'm on
>     PgSQL 8.1 (server on Debian) and 8.3 (devel on Ubuntu), in case it
>     matters.
>
>
> You certainly can do this, very similar to PS1 on linux with bash:
>
> http://www.postgresql.org/docs/8.1/static/app-psql.html#APP-PSQL-PROMPTING
>
> --Scott

That works like a charm, thank you!

Next question though;

How can I get it to save my custom prompt across sessions/server
restarts? It there something equivalent to '.bash_profile'?

Madi

Re: Adding the host name to the PgSQL shell

From
Scott Mead
Date:

On Mon, Jun 8, 2009 at 1:30 PM, Madison Kelly <linux@alteeve.com> wrote:

That works like a charm, thank you!

  No problem :) 


Next question though;

How can I get it to save my custom prompt across sessions/server restarts? It there something equivalent to '.bash_profile'?

  ~/.psqlrc

--Scott

Re: Adding the host name to the PgSQL shell

From
Madison Kelly
Date:
Scott Mead wrote:
>
> On Mon, Jun 8, 2009 at 1:30 PM, Madison Kelly <linux@alteeve.com
> <mailto:linux@alteeve.com>> wrote:
>
>
>     That works like a charm, thank you!
>
>
>   No problem :)
>
>
>
>     Next question though;
>
>     How can I get it to save my custom prompt across sessions/server
>     restarts? It there something equivalent to '.bash_profile'?
>
>
>   ~/.psqlrc

Thank you again!

For the record, in case someone finds this in an archive somewhere and
are a noob like me, you need to create this file in the home directory
of the shell user you call 'psql' from, *not* the user account used when
using the '-U user' switch. :)

Madi

Re: Adding the host name to the PgSQL shell

From
"Roderick A. Anderson"
Date:
Scott Mead wrote:
>
>
>
> On Mon, Jun 8, 2009 at 12:44 PM, Madison Kelly <linux@alteeve.com
> <mailto:linux@alteeve.com>> wrote:
>
>     Hi all,
>
>      I work on a development and production server, and I am always
>     double-checking myself to make sure I am doing something on the
>     right server.
>
>      Is there a way, like in terminal shells, to change the PgSQL
>     shell's prompt from 'db=>' to something like 'host@db=>'? I'm on
>     PgSQL 8.1 (server on Debian) and 8.3 (devel on Ubuntu), in case it
>     matters.
>
>
> You certainly can do this, very similar to PS1 on linux with bash:
>
> http://www.postgresql.org/docs/8.1/static/app-psql.html#APP-PSQL-PROMPTING

Thanks Scott.

I'll add that the "other" escape codes, well at least newline (\n) work.

So I have in my .psqlrc

    \set PROMPT1 '%/@%M\n%# '


And an expansion on the question.  I didn't see or find in the
documentation is if there is a substitution code to get the first schema
in search_path?


\\||/
Rod
--