• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

fphilipe/psql2csv: Run a query in psql and output the result as CSV.

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

fphilipe/psql2csv

开源软件地址:

https://github.com/fphilipe/psql2csv

开源编程语言:

Shell 100.0%

开源软件介绍:

psql2csv

Run a query in psql and output the result as CSV.

Installation

Mac OS X

psql2csv is available on Homebrew.

$ brew install psql2csv

Manual

Grab the file psql2csv, put in somewhere in your $PATH, and make it executable:

$ curl https://raw.githubusercontent.com/fphilipe/psql2csv/master/psql2csv > /usr/local/bin/psql2csv && chmod +x /usr/local/bin/psql2csv

Usage

psql2csv [OPTIONS] < QUERY
psql2csv [OPTIONS] QUERY

Options

The query is assumed to be the contents of STDIN, if present, or the last argument. All other arguments are forwarded to psql except for these:

-?, --help                 show this help, then exit
--delimiter=DELIMITER      set the field delimiter (default: ,)
--quote=QUOTE              set the quote delimiter (default: ")
--escape=ESCAPE            set the escape character (default: QUOTE)
--null=NULL                set the string representing NULL; printed without quotes (default: empty string)
--force-quote=FORCE_QUOTE  set the columns to be force quoted; comma separated list of columns or * for all (default: none)
--encoding=ENCODING        set the output encoding; Excel likes latin1 (default: UTF8)
--no-header                do not output a header
--timezone=TIMEZONE        set the timezone config before running the query
--search-path=SEARCH_PATH  set the search_path config before running the query
--dry-run                  print the COPY statement that would be run without actually running it

Example Usage

$ psql2csv dbname "select * from table" > data.csv

$ psql2csv dbname < query.sql > data.csv

$ psql2csv --no-header --delimiter=$'\t' --encoding=latin1 dbname <<sql
> SELECT *
> FROM some_table
> WHERE some_condition
> LIMIT 10
> sql

Advanced Usage

Let's assume you have a script monthly_report.sql that you run every month. This script has a WHERE that limits the report to a certain month:

WHERE date_trunc('month', created_at) = '2019-01-01'

Every time you run it you have to edit the script to change the month you want to run it for. Wouldn't it be nice to be able to specify a variable instead?

Turns out psql does have support for variables which you can pass to psql (and thus to psql2csv) via -v, --variable, or --set. To interpolate the variable into the query you can use :VAR for the literal value, :'VAR' for the value as a string, or :"VAR" for the value as an identifier.

Let's change the WHERE clause in monthly_report.sql file to use a variable instead:

WHERE date_trunc('month', created_at) = (:'MONTH' || '-01')::timestamptz

With this change we can now run the query for any desired month as follows:

$ psql2csv -v MONTH=2019-02 < monthly_report.sql > data.csv

Further Help

Author

Philipe Fatio (@fphilipe)




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap