0

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 ?

1

1 Answer 1

1

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$                                ↵
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, with what you gave me, i can retrieve what i needed without using \ef

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.