在 ubuntu 上将文本文件导入 Postgres?

Import a text file to Postgres on ubuntu?

我有一个很大的文本文件,格式为 "name:email"。 我还创建了一个包含名称和电子邮件列的 table。

如何将文本文件上传到 table?

文本文件在我的 ubuntu 服务器上,我已经使用命令

连接到 psql
sudo -u postgres psql
\connect <username>"

下一步我该怎么做才能将文本文件导入我的数据库?

psql 有一个 \COPY 命令

Copy allows you to do copy data into and out of tables in your database. It supports several modes including:

  • binary
  • tab delimited
  • csv delimited

您需要的是:

\COPY tablename(name,email) FROM '/path/to/file' DELIMITER ':' CSV

如果您遇到错误 ERROR: missing data for column "name",这意味着您的文件末尾有一个空行,只需将其删除即可。