如何将 PostgreSQL 查询输出导出到 csv 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29190632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:56:02  来源:igfitidea点击:

How to export a PostgreSQL query output to a csv file

postgresqlshellcsvexport-to-excelexport-to-csv

提问by mflowww

I'm having problem exporting my PostgreSQL output from a shell to a csv file.
My SQL script is called script.sql.

我在将 PostgreSQL 输出从 shell 导出到 csv 文件时遇到问题。
我的 SQL 脚本称为script.sql.

I typed the following command in my shell:

我在我的 shell 中输入了以下命令:

psql congress -af script.sql &> filename.csv

But when I opened the filename.csvfile, values of all the columns are squeezed in one column in the Excel csv (see the attached screenshot).

但是当我打开filename.csv文件时,所有列的值都被压缩在 Excel csv 中的一列中(请参阅附加的屏幕截图)。

Then I tried another way. I edited my script.sqlto be:

然后我尝试了另一种方式。我编辑我script.sql的是:

Copy (Select * From ...) To '/tmp/filename.csv' With CSV;

Then I typed the following command in the shell within the database dbname.

然后我在数据库中的 shell 中键入以下命令dbname

\i script.sql

The output is:

输出是:

COPY 162

Well, my output query has 162 rows.

好吧,我的输出查询有 162 行。

So the 162 rows of my output table have been copied in the shell. How can I paste or move them to a csv file?

所以我的输出表的 162 行已经被复制到 shell 中了。如何将它们粘贴或移动到 csv 文件?

Or, if I'm going to use the filename.csv (screenshot is attached), how can I fix the format of that csv/Excel file?

或者,如果我要使用 filename.csv(附上截图),我该如何修复该 csv/Excel 文件的格式?

Screenshot of filename.csv

文件名.csv 的屏幕截图

回答by Erwin Brandstetter

Modern syntax:

现代语法:

COPY (SELECT * FROM ...) TO '/tmp/filename.csv' (format CSV);

So the 162 rows of my output table have been copied in the shell. How can I paste or move them to a csv file?

所以我的输出表的 162 行已经被复制到 shell 中了。如何将它们粘贴或移动到 csv 文件?

The result isthe CSV file. Open it with any spreadsheet program using matching delimiters. Per documentation:

结果CSV 文件。使用任何使用匹配分隔符的电子表格程序打开它。根据文档:

The default is a tab character in text format, a comma in CSV format

默认为文本格式的制表符,CSV格式的逗号

Like Patrick commented, you can use the corresponding psql meta command \copyin a similar fashion. It writes (and reads) files local to the client and does not require superuser privileges.

就像帕特里克评论的那样,您可以\copy以类似的方式使用相应的 psql meta 命令。它写入(和读取)客户端本地的文件,不需要超级用户权限。

More explanation in these related answers:

这些相关答案中的更多解释:

回答by Ash

first copy your connection info into ~/.pgpass and

首先将您的连接信息复制到 ~/.pgpass 和

cat ip:port:dbname:user:pass > ~/.pgpass
chmod 0600 ~/.pgpass
psql -h serverip -U userid dbname -af test.sql | gzip > result.txt.gz

回答by Darren Gill

Going off on a bit of a tangent, there is another way too.

有点切线,还有另一种方式。

I use the following in a windows batch script: -

我在 Windows 批处理脚本中使用以下内容:-

psql.exe -U %cUser% -h %cHost% -p %cPort% -d %cDB% -t -o "%dumpDir%\tables.txt" -A -c "SELECT table_schema, table_name   FROM information_schema.tables WHERE table_schema = '%qSchema%';"

The trick is to remember the -A option. It suppresses whitespace padding of the data.

诀窍是记住 -A 选项。它抑制数据的空白填充。

I do this to avoid permission errors from the COPY command shown above with the account running postgress not have the same permissions as the account running my scheduled batch file.

我这样做是为了避免来自上面显示的 COPY 命令的权限错误,因为运行 postgress 的帐户与运行我的预定批处理文件的帐户没有相同的权限。

This gets me a list of tables in a given schema with results like:-

这为我提供了给定模式中的表列表,结果如下:-

myschema|mytable1

我的架构|我的表1

myschema|mytable2

myschema|mytable2

I then use the FOR batch command to process each line. If you REALLY wanted a CSV file, all you would need to do would be this:-

然后我使用 FOR 批处理命令来处理每一行。如果你真的想要一个 CSV 文件,你需要做的就是:-

ECHO table_schema,table_name > %dumpDir%\tables.csv
FOR /F "delims=|" %%p in (%dumpDir%\tables.txt) DO echo %%p,%%q >> %dumpDir%\tables.csv

Probably not the most efficient of mechanisms, but works fine on small numbers of output rows.

可能不是最有效的机制,但在少量输出行上工作正常。