- 创建文件,并设置777权限
cd /home/popo
touch export.csv
chmod 777 export.csv
-
使用超级账号postgres进入psql
psql -U postgres -d popo -p 5432
-
导出命令
copy (select * from student where age > 18) to '/home/popo/export.csv' with csv header;
若导出整个表, 用表名替换sql语句即可
copy student to '/home/popo/export.csv' with csv header;
-
copy命令还能用于导入数据到表中, 如:
copy student from '/data/test_data.csv' WITH delimiter ',' CSV HEADER encoding 'utf-8';
参数 | 解释 |
---|---|
delimiter ',' | 每一行字段的分隔符。csv文件一般使用逗号 |
csv header | 包括csv文件的标题,如果不需要标题行,直接忽略 |
encoding 'utf-8' | 指定编码 |
网友评论