从 PostgreSQL 复制结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6215700/
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
copy result from PostgreSQL
提问by Mateusz
I am copying every result of Query (application pgAdmin) into Google Doc's Sheet. I am wonder if it is possible to set Postgress to insert Tab instead of semicolon(;) to separate values in columns - I would like to simply copy and paste from postgres to Google Doc's sheet.
我正在将查询(应用程序 pgAdmin)的每个结果复制到 Google Doc's Sheet 中。我想知道是否可以将 Postgress 设置为插入 Tab 而不是分号(;)来分隔列中的值 - 我想简单地从 postgres 复制并粘贴到 Google Doc 的工作表。
If it is not possible, is there any way to write a macro in Google Doc's sheet?
如果不可能,有没有办法在 Google Doc 的工作表中编写宏?
I would like to ask one more question - how can I divide two values which I get by using select command (and how to demand from Postgres SQL to store values as for example double)?
我想再问一个问题 - 如何将使用 select 命令获得的两个值相除(以及如何要求 Postgres SQL 存储值,例如 double)?
回答by howMuchCheeseIsTooMuchCheese
Since JDBC within Apps Scriptdoesn't yet support Postgres (for some reason), you can't script this directly in Google Sheets. You could definitely script this the Google Sheets APIand the language of your choice (e.g. Python and Pandas make this a relatively simple task).
由于Apps Script中的JDBC尚不支持 Postgres(出于某种原因),因此您无法直接在 Google Sheets 中编写脚本。您绝对可以使用 Google Sheets API和您选择的语言编写脚本(例如 Python 和 Pandas 使这成为一项相对简单的任务)。
If you don't want to roll your own solution, check out SeekWell. It allows you to connect to databases and write SQL queries directly in Sheets. You can create a run a “Run Sheet” that will run multiple queries at once and schedule those queries to be run without you even opening the Sheet.
如果您不想推出自己的解决方案,请查看SeekWell。它允许您连接到数据库并直接在表格中编写 SQL 查询。您可以创建一个运行“运行表”,该运行表将一次运行多个查询并安排这些查询的运行,而无需打开该表。
Disclaimer: I made this.
免责声明:我做了这个。
回答by Steve Midgley
It is now possible to configure PgAdmin3 to copy/paste with Tab as the field delimiter instead of ;
现在可以将 PgAdmin3 配置为使用 Tab 作为字段分隔符而不是 ;
From the PgAdmin3 MAIN window:
从 PgAdmin3 主窗口:
- File
- Options
- Query Tool section
- Results Grid area
- Click pull down next to "Result copy field separator"
- Choose "Tab" from the picklist
- Results Grid area
- 文件
- 选项
- 查询工具部分
- 结果网格区域
- 单击“结果复制字段分隔符”旁边的下拉菜单
- 从选项列表中选择“选项卡”
- 结果网格区域
You might have to restart PgAdmin for this to take effect. Works perfectly for pasting directly into Google spreadsheets!
您可能必须重新启动 PgAdmin 才能使其生效。非常适合直接粘贴到 Google 电子表格中!
And regarding your division question, if you just divide the two numbers, you might not always get what you expect - integers don't divide automatically and become a float result - you have to cast them:
关于你的除法问题,如果你只是将两个数字相除,你可能不会总是得到你所期望的 - 整数不会自动除法并成为浮点结果 - 你必须对它们进行转换:
select 1::numeric/2::numeric
This returns 0.50
这将返回 0.50
Whereas:
然而:
select 1/2
Returns 0 (not generally what you want)
返回 0(通常不是你想要的)
回答by Praveen Kumar
In PGAdmin 4, you can achieve this by executing your sql query and then press F8 and it will export the query results.
在 PGAdmin 4 中,您可以通过执行 sql 查询然后按 F8 来实现此目的,它将导出查询结果。
Step 2: Now select the query result and click download query result in CSV format as shown below:
第二步:现在选择查询结果,点击下载CSV格式的查询结果,如下图:
Step 3: Verify the query result
第三步:验证查询结果
You are done.
你完成了。
回答by Sean
psql
can do this in a number of ways:
psql
可以通过多种方式做到这一点:
% psql -At -F, -c 'SELECT * FROM my_table'
1,login,http://stackjet.com/login
2,mod3,https://127.0.0.1:5000/mod3/
3,mod2,http://127.0.0.1:5000/mod2/
4,logout,https://127.0.0.1:5000/logout
Escaping is an issue with that scheme. COPY
is your friend.
逃逸是该计划的一个问题。COPY
是你的朋友。
回答by Peter Eisentraut
Save output of COPY
or \copy
as a .tsv
file and load that using File/Import. Alternatively, you can use the CSV format, in the same way.
将COPY
或 的输出保存\copy
为.tsv
文件并使用文件/导入加载该文件。或者,您可以以相同的方式使用 CSV 格式。
回答by passing_by
Maybe something like this could be usefull :)
也许这样的事情可能有用:)
select relname || chr(9) || reltype || chr(9) || relam from pg_class
回答by dave paola
I would perform your insertions / interpolations using vanilla SQL and then use a tool like QueryClipsto get your data into the google sheet.
我会使用 vanilla SQL 执行您的插入/插值,然后使用像QueryClips这样的工具将您的数据放入谷歌表中。