pg除了可以通过dump的方式导入和导出。如果只是导出数据,可以直接使用copy
导出
COPY user TO '/tmp/data/test.csv' WITH csv;
COPY user(name,password) TO '/tmp/data/test.csv' WITH csv;
COPY (select * from user) TO '/tmp/data/test.csv' WITH csv;
COPY (select name,age from user) TO '/tmp/data/test.csv' WITH csv header;
注意,远程连接也只会导出文件的数据库所在的目录。
语法
COPY { table_name [ ( column_name [, ...] ) ] | ( query ) }
TO { 'filename' | PROGRAM 'command' | STDOUT }
[ [ WITH ] ( option [, ...] ) ]
导入
COPY user_1 FROM '/tmp/data/test.csv' WITH csv;
语法
COPY table_name [ ( column_name [, ...] ) ]
FROM { 'filename' | PROGRAM 'command' | STDIN }
[ [ WITH ] ( option [, ...] ) ]