简体   繁体   English

postgresql-使用命令导出选择查询结果

[英]postgresql - export select query result using command

As we can take a backup file of database using pg_dump command, similarly can we take backup of a select query result. 因为我们可以使用pg_dump命令来备份数据库的备份文件,所以类似地,我们也可以备份选择查询结果。

For example if i have a query select * from tablename; 例如,如果我有一个查询,请select * from tablename; then i want to take backup result of the query that can be restored somewhere. 那么我想获取可以在某处还原的查询的备份结果。

You can use something like 您可以使用类似

copy (select * from tablename) to 'path/to/file';

it will generate csv file with results very same manner as pg_dump does (in fact in plain mode it actually runs COPY commands) 它将生成与pg_dump完全相同的结果的csv文件(实际上,在纯模式下,它实际上运行COPY命令)

update 更新

and if you want DDL as well, you can 如果您也想要DDL,则可以

create table specname as select * from tablename

and then 接着

pg_dump -s specname 

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

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