将压缩的CSV文件导入PostgreSQL [英] Importing zipped CSV file into PostgreSQL

查看:209
本文介绍了将压缩的CSV文件导入PostgreSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的压缩csv文件(25gb),我想将它导入PostgreSQL 9.5版本。有没有快速的方法将zip或qzip文件导入postgres而不提取文件?

I have a big compressed csv file (25gb) and I want to import it into PostgreSQL 9.5 version. Is there any fast way to import zip or qzip file into postgres without extracting the file?

推荐答案

使用命名管道有一个老技巧(适用于Unix,不了解Windows)

There is an old trick to use a named pipe (works on Unix, don't know about Windows)


  • 创建命名管道: mkfifo / tmp / omyfifo

  • 将文件内容写入其中: zcat mycsv.csv.z> / tmp / omyfifo&

  • [来自psql] 从'/ tmp / omyfifo'复制mytable(col1,...)

  • [完成后]: rm / tmp / omyfifo

  • create a named pipe: mkfifo /tmp/omyfifo
  • write the file contents to it: zcat mycsv.csv.z > /tmp/omyfifo &
  • [from psql] copy mytable(col1,...) from '/tmp/omyfifo'
  • [when finished] : rm /tmp/omyfifo

背景中的 zcat 将阻止读者(此处: COPY 命令)将开始阅读,它将在EOF结束。 (或者如果阅读器关闭了管道)

The zcat in the backgound will block until a reader (here: the COPY command) will start reading, and it will finish at EOF. (or if the reader closes the pipe)

您甚至可以启动多个管道+ zcat对,这将被多个 COPY 语句。

You could even start multiple pipes+zcat pairs, which will be picked up by multiple COPY statements in your sql script.

这将来自pgadmin,但是fifo(+ zcat进程) )应存在于DBMS服务器运行的机器上。

This will work from pgadmin, but the fifo (+zcat process) should be present on the machine where the DBMS server runs.

BTW:使用netcat的类似技巧可用于从远程计算机读取文件(当然应该将文件写入网络套接字)

BTW: a similar trick using netcat can be used to read a file from a remote machine (which of course should write the file to the network socket)

这篇关于将压缩的CSV文件导入PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆