How to add a new psql command ?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to add a new psql command ?

Quentin Di-Fant
Hello,

I was wondering : is it possible to add directly a new psql command which can be run anytime in our PostgreSQL interpreter like any other normal psql commands ?

I explain myself : in the PostgreSQL interpreter, when the command "\h" or "\help" is written in the shell, we can see all the commands we have at our disposal. We can also of course write our proper functions, in python scripts for example, and run them direclty in one of our databases. But I was wondering if it was possible to add directly a command, like \copy, \select, .... For example, let's say I create a new command called \resume which will call my own-made python script, and I would want this command to be part of the list of commands we see when we type \h.

Is it even possible ? And if it's possible, how can I achieve that ?

Thanks for your attention and I thank you in advance for all the informations you will possibly give me.

Cordially.
Reply | Threaded
Open this post in threaded view
|

Re: How to add a new psql command ?

Pavel Stehule
Hi

čt 7. 2. 2019 v 11:40 odesílatel Quentin Di-Fant <[hidden email]> napsal:
Hello,

I was wondering : is it possible to add directly a new psql command which can be run anytime in our PostgreSQL interpreter like any other normal psql commands ?

I explain myself : in the PostgreSQL interpreter, when the command "\h" or "\help" is written in the shell, we can see all the commands we have at our disposal. We can also of course write our proper functions, in python scripts for example, and run them direclty in one of our databases. But I was wondering if it was possible to add directly a command, like \copy, \select, .... For example, let's say I create a new command called \resume which will call my own-made python script, and I would want this command to be part of the list of commands we see when we type \h.

Is it even possible ? And if it's possible, how can I achieve that ?

Thanks for your attention and I thank you in advance for all the informations you will possibly give me.

It is not possible. psql is not designed to be enhanced.

you can use psql variables for this purpose partially

postgres=# \set x 'select 1;'
postgres=# :x
┌──────────┐
│ ?column? │
╞══════════╡
│        1 │
└──────────┘
(1 row)





Cordially.
Reply | Threaded
Open this post in threaded view
|

Re: How to add a new psql command ?

Michel Pelletier
You can also shell out to a command with \! and send data one way using a combo of \gset and \setenv, then bundle up the whole "function" as a .sql file you include when you want it with \i.  For example here's a snippet I use to get the session pid, export it, then fire up another tmux pane running gdb attached to my session and then continue:

select pg_backend_pid() pid \gset
\setenv PID :'pid'
\! tmux split-window -h
\! tmux send-keys 'gdb /usr/bin/postgres ' $PID  'C-m' 'cont' 'C-m'

This trick is also useful to export snapshot ids to subprocesses that need read consistent views with each other.

-Michel


On Thu, Feb 7, 2019 at 3:03 AM Pavel Stehule <[hidden email]> wrote:
Hi

čt 7. 2. 2019 v 11:40 odesílatel Quentin Di-Fant <[hidden email]> napsal:
Hello,

I was wondering : is it possible to add directly a new psql command which can be run anytime in our PostgreSQL interpreter like any other normal psql commands ?

I explain myself : in the PostgreSQL interpreter, when the command "\h" or "\help" is written in the shell, we can see all the commands we have at our disposal. We can also of course write our proper functions, in python scripts for example, and run them direclty in one of our databases. But I was wondering if it was possible to add directly a command, like \copy, \select, .... For example, let's say I create a new command called \resume which will call my own-made python script, and I would want this command to be part of the list of commands we see when we type \h.

Is it even possible ? And if it's possible, how can I achieve that ?

Thanks for your attention and I thank you in advance for all the informations you will possibly give me.

It is not possible. psql is not designed to be enhanced.

you can use psql variables for this purpose partially

postgres=# \set x 'select 1;'
postgres=# :x
┌──────────┐
│ ?column? │
╞══════════╡
│        1 │
└──────────┘
(1 row)





Cordially.