PostgreSQL: Copy/import a user defined function from one database to another database

旧街凉风 提交于 2021-01-29 05:41:37

问题


I have a similar question like this, but for PostgreSQL. I am using PostgreSQL 9.5.12 (x64) on my Windows 10 (x64) machine. I have a function (written in plpgsql) my_func() in database my_db. Is it possible to import/copy this function to another database in postgres?


回答1:


In psql the \ef command will open the function in an editor you can then save it to a file... unfortunately the UI of the windows version of psql was pretty cruddy last time I checked.

a pg_dump of the database with --schema-only will also include all function definitions. you can then open it in an editor and copy the functions.




回答2:


You could use pg_get_functiondef to get function definition as follows:

SELECT pg_get_functiondef(oid) AS definition FROM pg_proc WHERE proname = 'my_func';


来源:https://stackoverflow.com/questions/50646705/postgresql-copy-import-a-user-defined-function-from-one-database-to-another-dat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!