Postgresql: interactive with psql "&"

Unfortunately, psql do not have interactive command like Oracle's SQLPlus does - "&'  . Using \prompt and \set is as close as it gets. Also, they need to be within a sql script to be called. The \prompt does not go through automatically.


\prompt 'username please: ' prompt_username
\set username_var ':prompt_username'
select * from pg_user where usename = :'prompt_username'

OR

\prompt 'username please: ' prompt_username
\set username_var '\'' :prompt_username '\''
select * from pg_user where usename = :username_var;


Put them into a sql file and call them from psql.


-bash-4.1$ psql -d vcdb -U vc -f usercheck.sql
username please: postgres
 usename  | usesysid | usecreatedb | usesuper | usecatupd | userepl |  passwd  | valuntil | useconfig
----------+----------+-------------+----------+-----------+---------+----------+----------+-----------
 postgres |       10 | t           | t        | t         | t       | ******** |          |
(1 row)

No comments:

Post a Comment