Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

May we interact with a psql script?

Tags:

postgresql

Can we do something like

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

in a psql script file ?

The system would wait until we enter something then echo the 'End of script' string.

like image 754
Luc M Avatar asked Sep 19 '10 15:09

Luc M


2 Answers

I just realized that internal doesn't mean variable defined into 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 command \echo, you don't need to add a ; at the end. In fact, if you add one when using \prompt, you get an error.

You can show use the value read from the stdin.

\echo 'Here\'s the value read from stdin : ' :my_user
like image 145
Luc M Avatar answered Oct 23 '22 22:10

Luc M


The COPY command can, perhaps, help interaction with stdin,

 COPY t(a) FROM stdin;

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 possibility of do a massive inputing task.

like image 2
Peter Krauss Avatar answered Oct 23 '22 23:10

Peter Krauss