Question

I want to obtain the code to edit a function as result of a query, and there is a psql command that gives you the code already generated : \ef

Is there anyway to get from a query the result of a psql command ?

Was it helpful?

Solution

you can use a parameter -E - that works for almost all \commands

bash-4.1$ psql -E postgres
psql (9.4devel)
Type "help" for help.

postgres=# \l
********* QUERY **********
SELECT d.datname as "Name",
       pg_catalog.pg_get_userbyid(d.datdba) as "Owner",
       pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding",
       d.datcollate as "Collate",
       d.datctype as "Ctype",
       pg_catalog.array_to_string(d.datacl, E'\n') AS "Access privileges"
FROM pg_catalog.pg_database d
ORDER BY 1;
**************************

 ....

But this doesn't work for \sf or \ef statements - these statements use a function pg_get_functiondef()

postgres=# SELECT pg_get_functiondef('pg_catalog.sin'::regproc);
                     pg_get_functiondef                      
─────────────────────────────────────────────────────────────
 CREATE OR REPLACE FUNCTION pg_catalog.sin(double precision)↵
  RETURNS double precision                                  ↵
  LANGUAGE internal                                         ↵
  IMMUTABLE STRICT                                          ↵
 AS $function$dsin$function$                                ↵

(1 row)
 postgres=# SELECT pg_get_functiondef('pg_catalog.sin(double  precision)'::regprocedure);
                     pg_get_functiondef                      
 ─────────────────────────────────────────────────────────────
  CREATE OR REPLACE FUNCTION pg_catalog.sin(double precision)↵
   RETURNS double precision                                  ↵
   LANGUAGE internal                                         ↵
   IMMUTABLE STRICT                                          ↵
   AS $function$dsin$function$                                ↵
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top