0

I have created a function function_1() in Postgres.
Now I want to create another similar function function_2() and copy the content of function_1().

Any Postgres command line can do that?

So far I only used copy&paste method in the command line to create function_2() (need to change the function name to "function_2" after paste)

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
Xianlin
  • 1,139
  • 5
  • 20
  • 34
  • i wanted to make a short hand alias for an internal function and used : `select pg_get_functiondef ( 'current_database'::regproc ) ;` ... did a bit editing and then used : `create function _db () returns name as $$current_database$$ language internal stable parallel safe strict ;` ... for example – sol Nov 24 '22 at 11:18

2 Answers2

3

pg_dump the function definition, or use the pg_get_functiondef function to get the function definition. Modifying it is up to you.

If you're trying to programmatically create functions you might be better off with PL/PgSQL's EXECUTE command and the format function to create dynamic functions.

It's pretty unusual to need to do this, suggesting you're probably doing something that's much more easily done another way.

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
3

If you are using the GUI pgAdmin, you can simply select the function in the object browser and open the reverse engineered SQL script in an SQL editor window.

There is a dedicated item in the options to automate it:

Options - Query tool - Query Editor - Copy SQL from main window to query tool

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228