如何在 Postgresql 上将表导出为带有标题的 CSV?

发布于 2024-07-26 19:02:44 字数 173 浏览 18 评论 0原文

我试图通过命令行将带有标题的 PostgreSQL 表导出到 CSV 文件,但是我让它导出到 CSV 文件,但没有标题。

我的代码如下所示:

COPY products_273 to '/tmp/products_199.csv' delimiters',';

I'm trying to export a PostgreSQL table with headings to a CSV file via command line, however I get it to export to CSV file, but without headings.

My code looks as follows:

COPY products_273 to '/tmp/products_199.csv' delimiters',';

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

柠檬心 2024-08-02 19:02:44
COPY products_273 TO '/tmp/products_199.csv' WITH (FORMAT CSV, HEADER);

手册中所述。

COPY products_273 TO '/tmp/products_199.csv' WITH (FORMAT CSV, HEADER);

as described in the manual.

允世 2024-08-02 19:02:44

从 psql 命令行:

\COPY my_table TO 'filename' CSV HEADER

末尾没有分号。

From psql command line:

\COPY my_table TO 'filename' CSV HEADER

no semi-colon at the end.

另类 2024-08-02 19:02:44

除了表名之外,您还可以编写一个查询来仅获取选定的列数据。

COPY (select id,name from tablename) TO 'filepath/aa.csv' DELIMITER ',' CSV HEADER;

具有管理员权限

\COPY (select id,name from tablename) TO 'filepath/aa.csv' DELIMITER ',' CSV HEADER;

instead of just table name, you can also write a query for getting only selected column data.

COPY (select id,name from tablename) TO 'filepath/aa.csv' DELIMITER ',' CSV HEADER;

with admin privilege

\COPY (select id,name from tablename) TO 'filepath/aa.csv' DELIMITER ',' CSV HEADER;
偏爱自由 2024-08-02 19:02:44

当我无权从 Postgres 写出文件时,我发现我可以从命令行运行查询。

psql -U user -d db_name -c "Copy (Select * From foo_table LIMIT 10) To STDOUT With CSV HEADER DELIMITER ',';" > foo_data.csv

When I don't have permission to write a file out from Postgres I find that I can run the query from the command line.

psql -U user -d db_name -c "Copy (Select * From foo_table LIMIT 10) To STDOUT With CSV HEADER DELIMITER ',';" > foo_data.csv
冷清清 2024-08-02 19:02:44

这有效

psql dbname -F , --no-align -c "SELECT * FROM TABLE"

This works

psql dbname -F , --no-align -c "SELECT * FROM TABLE"
葮薆情 2024-08-02 19:02:44

最简单的方法(使用 psql )似乎通过使用 --csv 标志:

psql --csv -c "SELECT * FROM products_273" > '/tmp/products_199.csv'

The simplest way (using psql) seems to be by using --csv flag:

psql --csv -c "SELECT * FROM products_273" > '/tmp/products_199.csv'
甲如呢乙后呢 2024-08-02 19:02:44

对于我使用的9.5版本,它会是这样的:

COPY products_273 TO '/tmp/products_199.csv' WITH (FORMAT CSV, HEADER);

For version 9.5 I use, it would be like this:

COPY products_273 TO '/tmp/products_199.csv' WITH (FORMAT CSV, HEADER);
不即不离 2024-08-02 19:02:44

该解决方案使用 \copy 对我有用。

psql -h <host> -U <user> -d <dbname> -c "\copy <table_name> FROM '<path to csvfile/file.csv>' with (format csv,header true, delimiter ',');"

This solution worked for me using \copy.

psql -h <host> -U <user> -d <dbname> -c "\copy <table_name> FROM '<path to csvfile/file.csv>' with (format csv,header true, delimiter ',');"
心舞飞扬 2024-08-02 19:02:44

以下是我如何使用 pgsl 连接到 Heroku PG 数据库使其在 power shell 中工作:

我必须首先将客户端编码更改为 utf8,如下所示: \encoding UTF8

然后将数据转储到 CSV 文件,如下所示:

\copy (SELECT * FROM my_table) TO  C://wamp64/www/spider/chebi2/dump.csv CSV DELIMITER '~'

我使用 ~ 作为分隔符,因为我不喜欢 CSV 文件,我通常使用 TSV 文件,但它不会让我添加 '\t' 作为分隔符,所以我使用 ~ 因为它很少使用的字符。

Heres how I got it working power shell using pgsl connnect to a Heroku PG database:

I had to first change the client encoding to utf8 like this: \encoding UTF8

Then dumped the data to a CSV file this:

\copy (SELECT * FROM my_table) TO  C://wamp64/www/spider/chebi2/dump.csv CSV DELIMITER '~'

I used ~ as the delimiter because I don't like CSV files, I usually use TSV files, but it won't let me add '\t' as the delimiter, so I used ~ because its a rarely used characeter.

你的呼吸 2024-08-02 19:02:44

COPY 命令不受限制。 受到限制的是将输出从 TO 定向到除 STDOUT 之外的任何位置。 但是,通过 \o 命令指定输出文件没有限制。

\o '/tmp/products_199.csv';
COPY products_273 TO STDOUT WITH (FORMAT CSV, HEADER);

The COPY command isn't what is restricted. What is restricted is directing the output from the TO to anywhere except to STDOUT. However, there is no restriction on specifying the output file via the \o command.

\o '/tmp/products_199.csv';
COPY products_273 TO STDOUT WITH (FORMAT CSV, HEADER);
还在原地等你 2024-08-02 19:02:44

复制(anysql 查询 datawanttoexport)到 'fileablsoutepathwihname' 分隔符 ',' csv 标头;

使用这个你也可以导出数据。

copy (anysql query datawanttoexport) to 'fileablsoutepathwihname' delimiter ',' csv header;

Using this u can export data also.

甜`诱少女 2024-08-02 19:02:44

我发布这个答案是因为这里给出的其他答案实际上都不适合我。 我无法在 Postgres 中使用 COPY,因为我没有正确的权限。 所以我选择“导出网格行”并将输出保存为UTF-8。

由于不同的原因,@Brian 给出的 psql 版本也不适合我。 它不起作用的原因是显然 Windows 命令提示符(我使用的是 Windows)正在自行干预编码。 我不断收到此错误:

错误:编码“WIN1252”中字节序列为 0x81 的字符在编码“UTF8”中没有等效项

我最终使用的解决方案是编写一个简短的 JDBC 脚本(Java),该脚本读取 CSV 文件并向我的 Postgres 表直接发出插入语句。 这可行,但如果不改变编码,命令提示符也会起作用。

I am posting this answer because none of the other answers given here actually worked for me. I could not use COPY from within Postgres, because I did not have the correct permissions. So I chose "Export grid rows" and saved the output as UTF-8.

The psql version given by @Brian also did not work for me, for a different reason. The reason it did not work is that apparently the Windows command prompt (I was using Windows) was meddling around with the encoding on its own. I kept getting this error:

ERROR: character with byte sequence 0x81 in encoding "WIN1252" has no equivalent in encoding "UTF8"

The solution I ended up using was to write a short JDBC script (Java) which read the CSV file and issued insert statements directly into my Postgres table. This worked, but the command prompt also would have worked had it not been altering the encoding.

遗弃M 2024-08-02 19:02:44

尝试这个:
“从 '\tmp\products_199.csv' DELIMITER ',' CSV 标头复制 products_273”

Try this:
"COPY products_273 FROM '\tmp\products_199.csv' DELIMITER ',' CSV HEADER"

浅沫记忆 2024-08-02 19:02:44

保存到服务器上的文件,请使用任何 SQL 客户端执行此操作:

COPY products_273 TO '/tmp/products_199.csv' WITH (FORMAT CSV, HEADER);

警告:这要求您是超级用户或< pg_write_server_files 角色。

请在此处查看此方法的完整文档。


保存到客户端上的文件,请使用psql执行此操作:

\COPY products_273 TO '/tmp/products_199.csv' WITH (FORMAT CSV, HEADER);

此方法不需要任何额外的权限。

请参阅此方法的完整文档 此处

To save to a file on the server do this with any SQL client:

COPY products_273 TO '/tmp/products_199.csv' WITH (FORMAT CSV, HEADER);

Warning: this requires you to be superuser or a member of the pg_write_server_files role.

See the full docs of this method here.


To save to a file on the client, do this with psql:

\COPY products_273 TO '/tmp/products_199.csv' WITH (FORMAT CSV, HEADER);

This method does not require any extra permissions.

See the full docs of this method here.

左岸枫 2024-08-02 19:02:44

pgAdmin 中,突出显示您的查询语句,就像使用 F5 执行时一样,然后按 F9 - 这将打开文件浏览器,以便您可以选择保存 CSV 的位置。

如果您使用的是 Azure Data Studio,说明如下:Azure Data Studio:另存为 CSV

我知道这不是一个通用的解决方案,但大多数时候您只想手动获取文件。

In pgAdmin, highlight your query statement just like when you use F5 to execute and press F9 - this will open the file browser so you can pick where you save your CSV.

If you are using Azure Data Studio, the instruction are here: Azure Data Studio: Save As CSV.

I know this isn't a universal solution, but most of the time you just want to grab the file by hand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文