psql2csv

Run a query in psql and output the result as CSV.
授权协议 MIT License
开发语言 JavaScript
所属分类 应用工具、 终端/远程登录
软件类型 开源软件
地区 不详
投 递 者 吴城
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

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 itexecutable:

$ 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 lastargument. 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 wantto 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 (andthus to psql2csv) via -v, --variable, or --set. To interpolate thevariable into the query you can use :VAR for the literal value, :'VAR' forthe 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 variableinstead:

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)

  • 1、生成csv格式文件样式 2 tank 11 31 3 zhang 11 32 4 tom 10 33 5 gao 11 34 6 tank 12 35 7 zhang 13 36 8 tom 14 37 9 gao 15 38 10

  • PostgreSQL 的 COPY 语句可以完成导入和导出 CSV 文件的功能(需要相关权限), 此外 psql 的 \copy 命令通过运行 COPY 语句也可实现类似的功能 创建一个测试表 tokyo2020 CREATE TABLE tokyo2020 (rank integer, team varchar(32), gold integer, silver integer, copper

  • 需求:导出数据库中数据,生成csv文件(定时任务去做) 入口:扫表操作 扫表的sql:(polljob.sql) select oid,jobId,batchCode,COALESCE(batchStartTime,'2000-01-01'),COALESCE(batchEndTime, '2030-01-01'),jobStatus,COALESCE(offsetOid,'0') from T_

  •       最近遇到一个需求,定时把csv文件导入到postgresql数据库,csv文件中包含几万行数据。查询到可以用命令行导入,使用的是命令行工具psql.exe文件,可是在操作的过程中无法实现自动输入口令这个环节。经过反复寻找资料,找到了解决方法,那就是把口令放在环境变量里。使用 set PGPASSWORD=xxxx,这是是预存储登录口令,写在命令行里,由于这样写有会暴露数据库访问的用户和

  • PostgreSQL 导入和导出 CSV 文件 语法 # psql -d 数据库 -U 用户名 # \encoding # show client_encoding; # set client_encoding = 'iso-8859-1'; //CSV文件字符格式,不然会乱码 # COPY 表名 FROM 'CSV文件' WITH DELIMITER ',' NULL AS '' CSV HEA

  • FreeBSD 7.0-RELEASE-i386 + PostgreSQL8.3 # psql -d 数据库 -U 用户名 # \encoding # show client_encoding; # set client_encoding = 'iso-8859-1'; //CSV文件字符格式,不然会乱码 # COPY 表名 FROM 'CSV文件' WITH DELIMITER ',' NULL

  • 如果只是有一列数据,想把这列数据导入到数据库表里,表其他字段随意填的情况下,先将这列数据复制到excel表里,同时完善其他列。选择另存为CSV(逗号分隔) 登录数据库 psql -U 用户名 -d 数据库名 导入csv文件 copy 表名 from 'csv文件全路径' DELIMITER ',' csv header 如果导入csv文件报错 invalid byte sequence for e

  • $ createdb old_cms $ psql old_cms $ pg_restore --verbose --clean --no-acl --no-owner -h localhost -U aaa -d old_cms ~/Downloads/old_cms.dump # select * from pg_tables; # select * from "BlogPost"; # se

  • 获取命令行帮助 [postgres@pg ~]$ /opt/pgsql/bin/psql --help psql is the PostgreSQL interactive terminal. Usage: psql [OPTION]… [DBNAME [USERNAME]] General options: -c, --command=COMMAND run only single comma

  • Linux的新手。 我正在尝试将.csv复制到PostgreSQL数据库中, copy address from 'address.csv' delimiter ',' csv header; 我在psql提示符下使用 cd [目录]命令来cd到文件的位置。 操作失败。 我已经关闭了该终端窗口,并在另一个终端窗口cd中找到了数据文件夹,然后从那里打开了psql命令。 \! pwd显示文件存储文件夹

  • PostgreSQL带有易于使用的导出工具,可以从PostgreSQL数据库导出数据。 在本教程中,我们向您展示如何将数据从PostgreSQL导出到文件或csv文件。 1.连接PostgreSQL 使用psql命令连接到PostgreSQL数据库。 $ psql -p 5433 -U dba dbname PS 5433是我的PostgreSQL端口号。 2.出口准备就绪 输入“ \o /hom

  • 本文我们学习如何把csv数据文件导入至PostgreSQL中。 创建实例表和数据 首先创建persons表,包括五个字段: id first_name last_name dob : 出生日期 email CREATE TABLE persons ( id SERIAL, first_name VARCHAR(50), last_name VARCHAR(50), dob DATE

  • DROP TABLE IF EXISTS "BH"."BHpointxx"; CREATE TABLE "BH"."BHpointxx" ( ShipName TEXT, IMO varchar(60) , ShipTypeEN TEXT, Speed TEXT, Lon TEXT, Lat TEXT, Dest TEXT, ETA

  • 需求:在代码中用pgsql命令导入海量csv数据。因为数据量大,用传统insert的方式效率太低,所以这里采用psql命令在登录下用 PostgreSQL 的 COPY 命令导入CSV, 命令如下,具体到每个命令的意思,大家自行搜索哈。 # 导入 copy from PGPASSWORD=123456 psql -h 127.0.0.1 -p 5432 -d db_name -U postgre

相关阅读

相关文章

相关问答

相关文档