简体   繁体   English

如何仅在 Postgres 中备份函数

[英]How to take backup of functions only in Postgres

I want to take backup of all functions in my postgres database.How to take backup of functions only in Postgres?我想备份我的 postgres 数据库中的所有函数。如何只备份 Postgres 中的函数?

use pg_getfunctiondef ;使用pg_getfunctiondef see system information functions .查看系统信息功能 pg_getfunctiondef was added in PostgreSQL 8.4. pg_getfunctiondef是在 PostgreSQL 8.4 中添加的。

SELECT pg_get_functiondef('proc_name'::regproc);

To dump all functions in a schema you can query the system tables in pg_catalog ;要转储模式中的所有函数,您可以查询pg_catalog中的系统表; say if you wanted everything from public :说如果你想要public的一切:

SELECT pg_get_functiondef(f.oid)
FROM pg_catalog.pg_proc f
INNER JOIN pg_catalog.pg_namespace n ON (f.pronamespace = n.oid)
WHERE n.nspname = 'public';

it's trivial to change the above to say "from all schemas except those beginning with pg_ " instead if that's what you want.如果您想要的话,将上面的内容更改为“从除以pg_的所有模式之外的所有模式”是微不足道的。

In psql you can dump this to a file with:psql中,您可以将其转储到文件中:

psql -At dbname > /path/to/output/file.sql <<"__END__"
... the above SQL ...
__END__

To run the output in another DB, use something like:要在另一个数据库中运行输出,请使用类似:

psql -1 -v ON_ERROR_STOP -f /path/to/output/file.sql target_db_name

If you're replicating functions between DBs like this, though, consider storing the authorative copy of the function definitions as a SQL script in a revision control system like svn or git, preferably packaged as a PostgreSQL extension.但是,如果您像这样在 DB 之间复制函数,请考虑将函数定义的权威副本作为 SQL 脚本存储在修订控制系统(如 svn 或 git)中,最好打包为 PostgreSQL 扩展。 See packaging extensions .请参阅打包扩展

You can't tell pg_dump to dump only functions.你不能告诉pg_dump只转储函数。 However, you can make a dump without data ( -s or --schema-only ) and filter it on restoring.但是,您可以在没有数据的情况下进行转储( -s--schema-only )并在恢复时对其进行过滤。 Note the --format=c (also -Fc ) part: this will produce a file suitable for pg_restore .注意--format=c (也是-Fc )部分:这将生成一个适合pg_restore的文件。

First take the dump:首先采取转储:

pg_dump -U username --format=c --schema-only -f dump_test your_database

Then create a list of the functions:然后创建函数列表:

pg_restore --list dump_test | grep FUNCTION > function_list

And finally restore them ( -L or --use-list specifies the list file created above):最后恢复它们( -L--use-list指定上面创建的列表文件):

pg_restore -U username -d your_other_database -L function_list dump_test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM