Can we interact with psql script?

Can we do something like

\echo 'Type username to show its properties'; SELECT * FROM mY_users WHERE username = ?; \echo 'End of script'; 

in psql script file?

The system will wait for us to enter something, and then move on to the line "End of line script".

+4
source share
3 answers

I just realized that internal does not mean a variable defined in postgresql.conf.

So I can use \prompt

 \prompt 'Please, enter an username ', my_user SELECT * FROM mY_users WHERE username = :my_user; \echo 'End of script' 

EDIT

Like the \ echo command, you do not need to add ; In the end. In fact, if you add it when using \prompt , you will get an error message.

You can show the value read from stdin.

 \echo 'Here\ the value read from stdin : ' :my_user 
+6
source

The COPY command may possibly help interact with stdin,

  COPY t(a) FROM stdin; 

In this example, do the same as

 \prompt 'Please, enter a string ', mystr insert into t(a) values ( ':mystr' ); 

with less confusion with quotes and the ability to perform a massive input task.

+2
source
-1
source

All Articles