I'm using PostgreSQL version 8.2 and trying to save all functions/views source code as each files on a local. But it seems to be impossible to extract each function's code to each files.
example:
I'd like to save source files like below:
function a1, a2, a3 -----> a1.sql, a2.sql, a3.sql
How can I do it?
This is not a complete solution, but should get you started in the right direction.
Assuming you are using
psqlfor this, you can save the source code of a single function by spooling the output ofselect procsrc from pg_proc where proname = '...'to a file, e.g.:The
\t \aconfigurespsqlto only output the content of the columnprosrc. The\o a1.sqlredirects the query result to a file.As you want one file per function, you need to run this for each function. You can either do this manually or generate a script which does this by running a SQL statement:
The query generates the first script for each procedure, then runs the generated script.
Not tested, you probably need to tweak that to meet your needs.
Note that the content of
prosrcis NOT the complete function.It lacks the return type, the arguments and the language definition. In more modern Postgres versions you could use
pg_get_functiondef()for that, but with 8.2 you will need to put all those things together using the columns frompg_proc. The most complicated part is getting the function arguments right (because Postgres 8.2 also lacks,pg_get_function_arguments())Without the arguments, it will be something like:
instead of "just"
select prosrc .... from pg_proc.