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

查看:36
本文介绍了将压缩的 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 &
  • [from psql] copy mytable(col1,...) from '/tmp/omyfifo'
  • [完成时]: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 对,它们将被 sql 脚本中的多个 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.

顺便说一句:使用 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天全站免登陆