Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
T

trdsql

> 数据库
Open source

CLI tool that can execute SQL queries on CSV, LTSV, JSON, YAML and TBLN. Can output to various formats.

2.2K stars0 likes1 views
WebsiteGitHub

About

CLI tool that can execute SQL queries on CSV, LTSV, JSON, YAML and TBLN. Can output to various formats.

trdsql

trdsql is a CLI tool that can execute SQL queries on CSV, LTSV, JSON, YAML and TBLN files.

This tool is similar to others such as q and textql, with a key distinction: it allows the use of PostgreSQL or MySQL syntax.

For usage as a library, please refer to the godoc and the provided examples.

    1. INSTALL
    • 1.1. go get
      • 1.1.1. Requirements
    • 1.2. Download binary
    • 1.3. Homebrew
    • 1.4. MacPorts
    • 1.5. FreeBSD
    • 1.6. Cgo free
    1. Docker
    • 2.1. Docker pull
    • 2.2. image build
    • 2.3. Docker Run
    1. Usage
    • 3.1. Global options
    • 3.2. Input formats
      • 3.2.1. Input options
    • 3.3. Output formats
      • 3.3.1. Output options
    • 3.4. Handling of NULL
    • 3.5. Multiple queries
    1. Example
    • 4.1. STDIN input
    • 4.2. Multiple files
    • 4.3. Compressed files
    • 4.4. Output file
    • 4.5. Output compression
    • 4.6. Guess by output file name
    • 4.7. Columns is not constant
    • 4.8. TSV (Tab Separated Value)
    • 4.9. LTSV (Labeled Tab-separated Values)
    • 4.10. JSON
      • 4.10.1. jq expression
    • 4.11. JSONL(NDJSON)
    • 4.12. YAML
    • 4.13. TBLN
    • 4.14. WIDTH
    • 4.15. TEXT
    • 4.16. logfmt
    • 4.17. Raw output
    • 4.18. ASCII Table & MarkDown output
    • 4.19. Vertical format output
    1. SQL
    • 5.1. SQL function
    • 5.2. JOIN
    • 5.3. PostgreSQL
      • 5.3.1. Function
      • 5.3.2. Join table and CSV file is possible
    • 5.4. MySQL
    • 5.5. Analyze
    • 5.6. Configuration
    1. Library
    1. See also
    1. Learn More
    1. License

1. INSTALL

1.1. go get

bash
go get -d github.com/noborus/trdsql
cd $GOPATH/src/github.com/noborus/trdsql
make
make install

1.1.1. Requirements

go 1.25 or higher.

1.2. Download binary

Download binary from the releases page(Linux/Windows/macOS).

1.3. Homebrew

bash
brew install trdsql

1.4. MacPorts

bash
sudo port selfupdate
sudo port install trdsql

1.5. FreeBSD

freshports

bash
pkg install trdsql

1.6. Cgo free

Typically, go-sqlite3 is used for building. However, if you're building with CGO_ENABLED=0, consider using sqlite instead.

Building without CGO (CGO Free) can reduce issues related to cross-compiling, but it may result in slower execution times.

2. Docker

2.1. Docker pull

Pull the latest image from the Docker hub.

bash
docker pull noborus/trdsql

2.2. image build

Or build it yourself.

bash
docker build -t trdsql .

2.3. Docker Run

Docker run.

bash
docker run --rm -it -v $(pwd):/tmp trdsql [options and commands]

3. Usage

To use trdsql, you can either specify an SQL query or simply provide a file for conversion.

bash
trdsql [options] SQL

For file conversion, this is equivalent to executing 'trdsql -o[output format] "SELECT * FROM filename"'.

bash
trdsql -o[output format] -t [input filename]

3.1. Global options

  • -a filename analyze the file and suggest SQL.
  • -A filename analyze the file but only suggest SQL.
  • -config filename configuration file location.
  • -db db name specify db name of the setting.
  • -dblist display db list of configure.
  • -driver string database driver. [ mysql | postgres | sqlite3 | sqlite(CGO Free) ] (default "sqlite3")
  • -dsn string database driver specific data source name.
  • -debug debug print.
  • -help display usage information.
  • -q filename read query from the specified file.
  • -t filename read table name from the specified file.
  • -version display version information.

3.2. Input formats

  • -ig guess format from extension. (default)
  • -icsv CSV format for input.
  • -ijson JSON format for input.
  • -iltsv LTSV format for input.
  • -iyaml YAML format for input.
  • -itbln TBLN format for input.
  • -iwidth width specification format for input.
  • -itext text format for input.
  • -ilogfmt logfmt format for input.

3.2.1. Input options

  • -ih the first line is interpreted as column names(CSV only).
  • -id character field delimiter for input(default ",")(CSV only).
  • -ijq string jq expression string for input(JSON/JSONL only).
  • -ilr int limited number of rows to read.
  • -inull string value(string) to convert to null on input.
  • -inum add row number column.
  • -ir int number of rows to preread. (default 1)
  • -is int skip header row.

3.3. Output formats

  • -ocsv CSV format for output. (default)
  • -otsv TSV format for output.
  • -ojson JSON format for output.
  • -ojsonl JSONL(JSON Lines) format for output.
  • -oltsv LTSV format for output.
  • -oat ASCII Table format for output.
  • -omd Markdown format for output.
  • -oraw Raw format for output.
  • -ovf Vertical format for output.
  • -oyaml YAML format for output.
  • -otbln TBLN format for output.

Or, guess the output format by file name.

3.3.1. Output options

  • -out filename output file name.
  • -out-without-guess output without guessing (when using -out).
  • -oh output column name as header.
  • -od character field delimiter for output. (default ",")(CSV and RAW only).
  • -oq character quote character for output. (default """)(CSV only).
  • -oaq enclose all fields in quotes for output(CSV only).
  • -ocrlf use CRLF for output. End each output line with '\r\n' instead of '\n'."(CSV only).
  • -onowrap do not wrap long columns(AT and MD only).
  • -onull value(string) to convert from null on output.
  • -oz string compression format for output. [ gzip | bz2 | zstd | lz4 | xz ]

3.4. Handling of NULL

NULL is undecided in many text formats. JSON null is considered the same as SQL NULL. For formats other than JSON, you must specify a string that is considered NULL. In most cases you will need to specify an empty string ("").

If -inull "" is specified, an empty string will be treated as SQL NULL.

SQL NULL is an empty string by default. Specify the -onull "(NULL)" option if you want a different string.

bash
$ echo "1,,v" | trdsql -inull "" -onull "(NULL)" "SELECT * FROM -"
1,(NULL),v

In the case of JSON, null is NULL as it is, and the specified string is converted to NULL.

bash
$ echo '[1,null,""]' | trdsql -inull "" -ojson -ijson "SELECT * FROM -"
[
  {
    "c1": "1"
  },
  {
    "c1": null
  },
  {
    "c1": null
  }
]

3.5. Multiple queries

Multiple queries can be executed by separating them with a semicolon. Update queries must be followed by a SELECT statement.

bash
$ trdsql "UPDATE SET c2='banana' WHERE c3='1';SELECT * FROM test.csv"
1,Orange
2,Melon
3,banana

You can perform multiple SELECTs, but the output will be in one format.

bash
$ trdsql -oh "SELECT c1,c2 FROM test.csv;SELECT c2,c1 FROM test.csv"
c1,c2
1,Orange
2,Melon
3,Apple
c2,c1
Orange,1
Melon,2
Apple,3

4. Example

test.csv file.

csv
1,Orange
2,Melon
3,Apple

Please write a file name like a table name.

bash
trdsql "SELECT * FROM test.csv"

-q filename can execute SQL from file

bash
trdsql -q test.sql

4.1. STDIN input

"-" or "stdin" is received from standard input instead of file name.

bash
cat test.csv | trdsql "SELECT * FROM -"

or

bash
cat test.csv | trdsql "SELECT * FROM stdin"

4.2. Multiple files

Multiple matched files can be executed as one table.

bash
$ trdsql -ih "SELECT * FROM tt*.csv"
1,test1
2,test2
3,test3

[!NOTE] It is not possible to mix different formats (ex: CSV and LTSV).

4.3. Compressed files

If the file is compressed with gzip, bz2, zstd, lz4, xz, it will be automatically uncompressed.

bash
trdsql "SELECT * FROM testdata/test.csv.gz"
bash
trdsql "SELECT * FROM testdata/test.csv.zst"

It is possible to mix uncompressed and compressed files using wildcards.

bash
trdsql "SELECT * FROM testdata/test.csv*"

4.4. Output file

-out filename option to output the file to a file.

bash
trdsql -out result.csv "SELECT * FROM testdata/test.csv ORDER BY c1"

4.5. Output compression

-oz compression type to compress and output.

bash
trdsql -oz gz "SELECT * FROM testdata/test.csv ORDER BY c1" > result.csv.gz

4.6. Guess by output file name

The filename of -out filename option determines the output format(csv, ltsv, json, tbln, raw, md, at, vf, jsonl) and compression format(gzip, bz2, zstd,lz4, xz) by guess.

Guess by extension output format + output compression (eg .csv.gz, .ltsv.lz4, .md.zst ...).

The following is an LTSV file compressed in zstd.

bash
trdsql -out result.ltsv.zst "SELECT * FROM testdata/test.csv"

4.7. Columns is not constant

If the number of columns is not a constant, read and decide multiple rows.

bash
$ trdsql -ir 3 -iltsv "SELECT * FROM test_indefinite.ltsv"
1,Orange,50,,
2,Melon,500,ibaraki,
3,Apple,100,aomori,red

4.8. TSV (Tab Separated Value)

-id "\\t" is input from TSV (Tab Separated Value)

tsv
1	Orange
2	Melon
3	Apple
bash
trdsql -id "\t" "SELECT * FROM test-tab.csv"

-otsv is TSV (Tab Separated Value) output.

bash
$ trdsql -otsv "SELECT * FROM test.csv"
1	Orange
2	Melon
3	Apple

4.9. LTSV (Labeled Tab-separated Values)

-iltsv is input from LTSV(Labeled Tab-separated Values).

sample.ltsv

ltsv
id:1	name:Orange	price:50
id:2	name:Melon	price:500
id:3	name:Apple	price:100
bash
trdsql -iltsv "SELECT * FROM sample.ltsv"
ltsv
1,Orange,50
2,Melon,500
3,Apple,100

[!NOTE] Only the columns in the first row are targeted.

-oltsv is LTSV(Labeled Tab-separated Values) output.

bash
$ trdsql -iltsv -oltsv "SELECT * FROM sample.ltsv"
id:1	name:Orange	price:50
id:2	name:Melon	price:500
id:3

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Goclicsvgogolang

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库