![](/img/trans.png)
[英]psql : load query from file and output psql messages to log file
[英]psql - write a query and the query's output to a file
在postgresql 9.3.1中,當使用psql命令交互式開發查詢時,最終結果有時會將查詢結果寫入文件:
boron.production=> \o /tmp/output
boron.production=> select 1;
boron.production=> \o
boron.production=> \q
$ cat /tmp/output
?column?
----------
1
(1 row)
這很好用。 但是,如何將查詢本身與查詢結果一起寫入文件?
我試過給psql --echo-queries
開關:
-e, --echo-queries
Copy all SQL commands sent to the server to standard output as well.
This is equivalent to setting the variable ECHO to queries.
但這總是與stdout相呼應,而不是我用\\ o命令給出的文件。
我也試過了--echo-all
開關,但它似乎沒有回應交互式輸入。
使用命令編輯,我可以在前面用\\qecho
重復查詢。 這很有效,但很乏味。
有沒有辦法指導交互式psql會話將查詢和查詢輸出寫入文件?
您可以嘗試直接從shell重定向stdout到文件(Win或Linux應該工作)
psql -U postgres -c "select 1 as result" -e nomedb >> hello.txt
這樣做的缺點是不允許您以交互方式查看輸出。 如果這是一個問題,您可以在單獨的終端中拖尾輸出文件,或者,如果在* nix中,則使用tee
實用程序:
psql -U postgres -c "select 1 as result" -e nomedb | tee hello.txt
希望這可以幫助!
盧卡
我知道這是一個老問題,但至少在9.3和當前版本中,這可以使用文檔中顯示的查詢緩沖區元命令或\\? 來自psql控制台: https : //www.postgresql.org/docs/9.3/static/app-psql.html
\w or \write filename
\w or \write |command
Outputs the current query buffer to the file filename or pipes it to the shell command command.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.